salep
salep

Reputation: 1380

Express.js response is not defined error

I'm trying to implement a simple login, but there's a small issue. bcrypt returns a response, so when I try to res.send("stuff"), it returns an error since it thinks that I'm trying to access a property of the res object rather than express.js's response feature. (correct me if I'm wrong).

ReferenceError: response is not defined at 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");
        }
    });
};

Upvotes: 0

Views: 2836

Answers (1)

Furkan Başaran
Furkan Başaran

Reputation: 1947

Second argument res in compare method's callback, conflict with express's response object res, you can't declare same variable name in one scope.

just update your code like below:

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");
                }
            });

Upvotes: 1

Related Questions