如何在多个条件下执行此 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) {
最佳答案
尝试使用 mongoose promise 系统 (.exec()
)。 query .findOne()
内部应该是一个对象。
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
}
...
});
关于node.js - 如何在多个条件下做简单的 Mongoose findOne?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780524/