Search code examples
mysqlnode.jseveryauth

Why does this code with nodejs, and everyauth crash with an empty array from MySQL?


I have some code that connects to a MySQL db to auth any user... this code works fine. The problem is when I receive an empty array from MySQL, it crashes nodejs...

This is part of the code in everyauth... sql is a exported function...

var promise = this.Promise();

sql.getUserInfo(userInfo, function(user, err) {
    if(err) return promise.fulfill([ err ]);

    promise.fulfill(user);
});

return promise;

I have no idea what promise is, but don't think that's the problem. This is the error in console when nodejs crashes:

starting step - extractLoginPassword
...finished step
starting step - authenticate
[]//--- it cames from mysql when there is no user and password 

timers.js:96
            if (!process.listeners('uncaughtException').length) throw e;

This is the exported function that calls the db and extracts information:

exports.getUserInfo = function(user, callback) {
  var email = user.email;
  var passwd = user.password;
  var sql = "SELECT id_email, contrasena FROM usuarios.usuario WHERE id_email = ? AND contrasena = ? ";
  var params = [email, passwd];

  client.query(sql, params, function(err, rows) {

    if (err) return callback(null, err);
    if (rows && rows.length > 0) {
      var user = rows[0];

      callback(user,err);
    }

Solution

  • Just check if query actually returns something, and fulfill some error if it is

    sql.getUserInfo(userInfo, function(user, err) {
                    if(err) return promise.fulfill([ err ]);
                    if(!user) return promise.fulfill(['Authentication error']);
                    promise.fulfill(user);
                });
    

    should work fine ^