node.js - 如何在多个条件下做简单的 Mongoose findOne?

标签 node.js express mongoose find conditional-statements

如何在多个条件下执行此 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/

相关文章:

php - 拉拉维尔 5.4 "npm run dev"

javascript - 在javascript中,如何将十进制(带小数点)转换为十六进制字符串

javascript - 初学者 Node.JS,未按预期返回

node.js - 如果未找到 ID,Mongoose 更新文档或插入新文档(Express REST API)

javascript - Mongodb:无法使用字符串字段名称附加到数组

node.js - 在 Node 上安装 Mongoose

node.js - 从派生命令管道输出时如何返回错误?

javascript - Express 4 ans Socket.io - 在模块中访问 app.io

javascript - 类型 'Document' 缺少类型中的以下属性

node.js - 使用nodejs或express render为swig设置全局变量