2012-11-08 34 views
0

我正在試圖通過創建自己的貓鼬帳戶後,註銷用戶時未定義功能錯誤...在路線/ index.js看起來護照註冊錯誤與user.login

TypeError: undefined is not a function 
at /node_modules/passport/lib/passport/http/request.js:44:48 
at pass (/node_modules/passport/lib/passport/index.js:240:14) 
at Passport.serializeUser (/node_modules/passport/lib/passport/index.js:242:5) 
at IncomingMessage.req.login.req.logIn (/node_modules/passport/lib/passport/http/request.js:43:29) 
at Promise.<anonymous> (/routes/index.js:33:25) 
at Promise.addBack (/node_modules/mongoose/lib/promise.js:120:8) 

我的寄存器功能像這樣:

exports.register = function (req, res) { 
// Generate salt 
Common.bcrypt.genSalt(function(err, salt) { 
    req.body.salt = salt; 
    // Generate hash 
    Common.bcrypt.hash(req.body.password, salt, function(error, hash) { 
     req.body.hash = hash; 
     // Remove clear text password 
     delete req.body.password; 
     // Save new user 
     new Model.User(req.body).save(function(err, user) { 
      console.log(err); 
      console.log(req); 
      if (user) { 
       req.login(user); 
       req.redirect('/'); 
      } else { 
       res.json(false); 
      } 
     }); 

    }); 
}); 

任何想法?

回答

2

如果你看看node_modules/passport/lib/passport/http/request.js,第44行第48列,你會看到功能done正在被調用。錯誤告訴你undefined is not a function,所以done沒有定義。如果您查看同一文件的第29行,您會看到done是您應該傳遞給req.login函數的參數。換句話說,它期待回調,而你沒有提供回調。也許是這樣的:

if (user) { 
    req.login(user, function(err) { 
    if (err) { 
     return req.send('FAILBOAT!'); 
    } 
    return req.redirect('/'); 
    }); 
} 

雖然我用的護照,我不叫req.login直接,所以我不100%以上代碼的含義確定。我還會指出,我在我的用戶模型中完成了所有的bcrypt工作,這似乎是解決問題的一個更清晰的方法。

+0

似乎解決了這些錯誤,但現在它說登錄失敗:錯誤:無法序列化用戶進入會話。 當我剛剛登錄時,會話序列化工作正常,但是當我調用req.login方法時,它有序列化問題。有任何想法嗎? –

+0

我在我的app.js中使用這個: passport.serializeUser(function(user,cb){cb(null,user.id)}); –

+0

我接受了你的答案 - 我發現當Mongoose返回保存時,它不會給我記錄的ID,所以我需要弄清楚如何讓它吐出來給我。 –