无法从 Go Fiber 会话存储中检索会话值

时间:2024-03-29

浏览:367

作者:佚名

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《无法从 Go Fiber 会话存储中检索会话值》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我正在使用 fiber golang 框架。我不明白为什么我无法从另一个请求或内部获取商店(在本例中为 redis)中设置的值。下面是代码:

sessionProvider := redis.New(redis.Config{
    KeyPrefix:   "session",
    Addr:        "127.0.0.1:6379",
    PoolSize:    8,
    IdleTimeout: 30 * time.Second,
})
sessions := session.New(session.Config{
    Provider: sessionProvider,
})

// sample routes for testing session
app.Get("/testing1", func(c *fiber.Ctx) {
    store := sessions.Get(c)
    //    set value to the session store
    store.Set("name", "King Windrol")
    store.Save()
})
app.Get("/testing2", func(c *fiber.Ctx) {
    store := sessions.Get(c)
    c.Send(store.Get("name"))
})

我尝试从同一个请求中获取它,它似乎在调用 store.save() 之前工作,但在调用之后不起作用!它只返回 nil


解决方案


app.Get("/testing1", func(c *fiber.Ctx) {
    store := sessions.Get(c)
    //    set value to the session store
    store.Set("name", "King Windrol")
    store.Save()
    c.SendStatus(200) //add this
    
})

以上就是《无法从 Go Fiber 会话存储中检索会话值》的详细内容,更多关于的资料请关注golang学习网公众号!

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

热门专题

更多
首页