2016-01-12 38 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