NodeJS Redis-Sessions" cb不是一个功能"

时间:2016-01-12 03:16:07

标签: node.js

我尝试使用Redis-Sessions模块来创建和存储会话数据。它在我使用Web套接字进行通信时有效,但我只是试图让它在服务器端页面加载时工作以检查用户是否已登录。我得到了这个奇怪的错误:

TypeError: cb is not a function at RedisSessions._handleError             (node_modules/redis-sessions/index.js:452:7) at RedisSessions._handleError     (node_modules/redis-sessions/index.js:19:61) at RedisSessions._validate     (node_modules/redis-sessions/index.js:554:20) at RedisSessions.get     (node_modules/redis-sessions/index.js:140:22) at RedisSessions.get     (node_modules/redis-sessions/index.js:19:61) at     Object.module.exports.getHomepage (server/pages.js:29:39) at next (native)
  at Object.dispatch (node_modules/koa-router/lib/router.js:331:14)
  at next (native)
  at Object.<anonymous> (node_modules/koa/node_modules/koa-compose/index.js:29:12)

代码类似于:

var RedisSessions = require("redis-sessions");
var rs = new RedisSessions();

module.exports = {
getHomepage: function*()
{
    var dataObj = yield initialiseSite();
    var sessionCookie = this.cookies.get('userid');
    console.log('sessionCookie='+sessionCookie);

    if(sessionCookie)
    {
        var sessionObj = yield rs.get({ app: 'appname', token: sessionCookie});
        console.log('sessionObj='+sessionObj)
        if(sessionObj)
            console.log('user is already logged in')
    }
}
}

可能是因为我使用的是koajs和收益率?

1 个答案:

答案 0 :(得分:1)

我真的对Redis API一无所知,但在简短地看了一下rs.get()的代码之后,它看起来好像在回调。在深入了解documentation之后,提供了以下示例:

rs.get({ app: 'appname', token: sessionCookie}, function(err, resp) {
   /*
    resp contains the session:

    {  
      "id":"user1001",
      "r": 2,  // The number of reads on this token
      "w": 2,  // The number of writes on this token
      "idle": 21,  // The idle time in seconds.
      "ttl": 7200, // Timeout after 7200 idle time
      "d":
         {
          "foo": "bar",
          "unread_msgs": 12,
          "last_action": "/read/news",
          "birthday": "2013-08-13"
        }
    }
    */
});

假设res.get()适用于yeild,我相信即使它是noop,您仍需要传递回调。