2016-03-03 51 views
0

如何在多個條件下執行此findOne查詢?或替代方法(最好是一個同樣簡單的),如果它是不可能的......如何做簡單貓鼬findOne有多個條件?

User.findOne({ 'email': email }, function (err, user) { 

     if (err) { 
      request.session.flash = { 
       type: "danger", 
       message: "Failed to log you in, error occurred." 
      }; 
      response.redirect("/snippet/"); 
     } 

我已經試過

User.findOne({ 'email': email, 'pwd': pwd }, function (err, user) { 

User.findOne({ 'email': email $and 'pwd': pwd }, function (err, user) { 

User.findOne({ 'email': email}, $and: {'pwd': pwd}, function (err, user) { 
+0

第一個之前您pwd變量運行相同的散列/加密你是如何做到的。什麼沒有關於它的工作? – JohnnyHK

回答

1

嘗試使用貓鼬承諾系統(.exec())。 .findOne()中的query應該是單個對象。

User 
    .findOne({email: email, pwd: pwd}) 
    .exec(function(err, user){ 
     ... 
    }); 

旁註 - 它看起來像是用於驗證用戶登錄。對電子郵件進行查詢可能是一種更好的策略,然後嘗試匹配密碼以提供更多深度的錯誤響應,而不是一攬子「無效登錄」類型響應。

User 
    .findOne({email: email}) //If not found, no user with that email exists 
    .exec(function(err, user){ 
     var hashed_pwd = hashPassword(pwd); 
     if(!hashed_pwd === user.pwd){ 
      //If they don't match, user entered wrong password 
     } 
     ... 
    }); 
1
User.findOne({ 'email': email, 'pwd': pwd }, function (err, user) { 

這個語法是正確的。如果這沒有返回任何結果,則表示您沒有匹配任何記錄。

我的猜測是,你pwd場被散列/加密,你需要在你已經試過是什麼列表這樣findOne