Express.js响应未定义错误

时间:2015-11-02 14:43:53

标签: node.js express

我尝试实施简单的登录,但这是一个小问题。 bcrypt返回response,因此当我尝试res.send("stuff")时,它会返回错误,因为它认为我试图访问res对象的属性而不是表达。 js的响应功能。 (如果我错了,请纠正我。)

  

ReferenceError:authController.js:79:21

中未定义响应
exports.loginModalPost = function (req, res) {
    // post
    var email = req.body.email;
    var password = req.body.password;

    // email & password validation.
    user.where('email', email).fetch().then(function (data) {
        if (data) {
            data = data.toJSON();
            bcrypt.compare(password, data.password_hash, function (err, res) {
                if(res === true) {
                    res.send("login is valid");
                } else {
                    // return err, but don't say that password is wrong to the end user.
                    res.send("password is wrong");
                }
            });
        } else {
            // email is wrong, no need to check password.
            // return err, but don't say that email is wrong.
            res.send("email is wrong");
        }
    });
};

1 个答案:

答案 0 :(得分:1)

res方法的回调中的第二个参数compare,与express的响应对象res冲突,你不能在一个范围内声明相同的变量名。

只需更新您的代码,如下所示:

bcrypt.compare(password, data.password_hash, function (err, result) {
                if(result === true) {
                    res.send("login is valid");
                } else {
                    // return err, but don't say that password is wrong to the end user.
                    res.send("password is wrong");
                }
            });