簡體   English   中英

NodeJS Redis-Sessions“ cb不是函數”

[英]NodeJS Redis-Sessions “cb is not a function”

我正在嘗試使用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和yields?

我真的對Redis API一無所知,但是,在簡短rs.get()的代碼后,看起來好像在期待回調。 在深入研究其文檔之后 ,將提供以下示例:

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 ,您仍然需要傳遞回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM