mongo查询始终返回true

时间:2015-10-24 16:13:25

标签: mongodb

mongo新手。试图查询数据库并且它总是返回我的console.log这个用户存在'不管他们是否真的这样做。它通过我的console.log确定登录路由中的功能。我该如何解决?这是我的代码:

var checkCredentials = function(userName, password) {
    console.log('made it to checkCredentials()');
    User.find({
        "name" : userName.toLowerCase(),
        "password" : password
    }),
    function(error, user) {
        console.log('made it to function in login route');
        if (error) {
            console.log(error)
        }
        if (userName.toLowerCase && typeof userName.toLowerCase && password && typeof password !== 'undefined') {
            console.log('this user exists');
        }
    }();
}

更新后的代码仍无效:

var checkCredentials = function(userName, password) {
    console.log('made it to checkCredentials()');
    User.findOne({
        "name" : userName.toLowerCase(),
        "password" : password
    }),
    function(error, user) {
        if(error){
            console.log(error);
        }
        console.log(user);
    }();
}

1 个答案:

答案 0 :(得分:0)

此代码最终正常运行。我不知道为什么它原来不起作用。我最终删除它,重新输入它,它工作。不确定是什么问题。

var checkCredentials = function(userName, password, res) {
    User.findOne({
        "name" : userName,
        "password" : password
    }, function(error, user) {
        if (error) {
            return handleError(error);
        } else if (user == null) {
            console.log('no match');
            res.send('noMatch');
        }
    });
}
相关问题