2016-01-12 102 views
0

我想使用Redis会话模块来创建和存储会话数据。它适用于我使用Web套接字进行通信时,但我试图让它在服务器端页面加载上工作以检查用户是否已登录。我得到这个奇怪的错误:NodeJS Redis会话“cb不是函数”

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

我真的不知道有关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

+0

您的权利。它不支持收益率。只是我习惯使用产量并把它们放在错误的地方。 – user3508995