node.js - Sails JS - session

标签 node.js session sails.js

每次重新加载页面时,我都会运行这个函数:

init: function(req, res)
{
    console.log("Session: ");
    console.log(req.session);

    if (req.session.player_id !== undefined)
    {
        Player.getPlayer(req.session.player_id);
    }
    else
    {
        req.session.player_id = sails.uuid.v4();
        req.session.save();
        console.log("Session saved");

        Player.createPlayer(req.session.player_id);
        console.log("Creating player: " + req.session.player_id);
    }
}

问题是每次我跑

    console.log("Session: ");
    console.log(req.session);

player_id 不存在,每次重新加载页面时,if 语句都会返回 false。

console.log("Creating player: " + req.session.player_id);

返回正确的值,但我不明白为什么每次页面重新加载时 session 都没有跟随用户?

最佳答案

看起来解决方案就这么简单:

res.send(req.session);

如果我将此添加到 else 语句的末尾, session 将被存储并可以重新使用。

为什么会这样,我也不知道。如果有人想详细说明,请成为我的客人。

关于node.js - Sails JS - session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31405910/

相关文章:

javascript - react native 和 socket.io Node 不起作用

javascript - 如何在 Loopback 中获取嵌套的主从或主-从-从查询

javascript - 用 jasmine .toEqual 比较两个不同的对象,一个对象是空的,另一个对象有一个 key 是一个符号,为什么它说它们相等?

node.js - 我应该 .npmignore 我的测试吗?

php - 使用 302 重定向/IMG SRC/JSON-P 共享 session 以及对 Google SEO/Pagerank 或其他问题的影响

node.js - 当 auth header 更改时 Express 创建不同的 session

java - 从 ServletContainer 或 ApplicationServer 检索打开的 session 数

node.js - SailsJS v0.10 多关联查找模型

javascript - 如何在 mongodb 中上传文件(不需要使用 BSON 的 gridfs )

javascript - 如何处理 Sails JS 返回的验证消息?