mishso
mishso

Reputation: 33

Bluebird promise resolve is being caught as an error

I am using promises to return data for a user do authenticate them and my resolve is showing up in my .catch for my call.

Call to the login function;

 function(req, username, password, done) {
        user.login(username, password).then( function(err, results){
            if (err) throw err;

            done(null, results.user);

        }).catch(
            function(err){
                console.log("Failed to log in", err);
                done(null, false);
            }
        );
}

Here is the promise code:

exports.login = function(username, password){
return new promise(function(resolve, reject){
    var sql = `CALL LOGIN(?)`;
    db.conn.query(sql, username, (err, results, fields) => {
            if (err) {
              reject("SQL ERR:", err);
            }

            var user = results[0][0];
            if (!user.uID) {
                reject("Incorrect username");
            }

            if(bcrypt.compareSync(password, user.pword)){
                resolve(user);
            } else {
                reject('Incorrect password');
            }

        });
});
}

When this is called, the user objet is being thrown as an error even though its being called on resolve... I am currently stuck, I tried to re-install the bluebird module as well incase something happened with it.

Upvotes: 0

Views: 224

Answers (1)

Arnold Gandarillas
Arnold Gandarillas

Reputation: 4322

What's going on here is the next thing:

Here you are passing the user object as a param if everything is ok

if (bcrypt.compareSync(password, user.pword)) {
  resolve(user);
} else {
  reject('Incorrect password');
}

So in this part .then of your code you are receiving a user object as a unique param so there is no necessity to check if there is an error.

function (req, username, password, done) {
  user.login(username, password).then(function(user) {
    // a good way to see all arguments is
    // console.log(arguments);
    done(null, user);
  }).catch(
    function(err) {
      console.log("Failed to log in", err);
      done(null, false);
    }
 );
}

Upvotes: 1

Related Questions