Zowak Zowak - 6 months ago 14
Node.js Question

async watefall doesn't call the functions

So i am actually woking on a simple program with node.Js and i have an issue using async.waterfall :


  • I created a function in my user model that connect the user by accessing the database, here is the code :

    exports.connection = function (login,password) {

    async.waterfall([
    function getLogin(callback){
    usersModel.findOne({ login: login }, function (err, res) {
    if (err){
    callback(err,null);
    return;
    }
    if(res != null ){
    // test a matching password if the user is found we compare both passwords
    var userReceived = res.items[0].login;
    callback(null,userReceived);
    }
    });
    },
    function getPassword(userReceived, callback){
    console.log(userReceived);
    callback(null,'done')
    }
    ], function(err){
    if (err) {
    console.error(err);
    }
    console.log('success');
    });
    }


    Using node-inspector i figured out that the main issue(I think) is that when it enters the waterfall function it doesn't execute the callback function of findOne it literally skips this and directly jump to the getPassword function (which isn't executed too).



so if someone could help me figuring out what's the problem that would be nice since i'm on it for around two days now.

Thank you

Answer

What I don't understand is - what was the flow exactly? did 'usersModel.findOne' get called?

What I see that is missing here in the getLogin function is a callback in the case that both the 'if' statement return false. in this case you'll get stuck in the first function and you won't advance to 'getPassword' function.

If this still doesn't work, please try executing the following code and report what was printed:

exports.connection = function (login,password) {
async.waterfall([
function getLogin(callback){
      usersModel.findOne({ login: login }, function (err, res) {
        if (err){
          console.log('getLogin - error has occured');
          callback(err,null);
          return;
        }
        if(!res)
        {
          console.log('getLogin - returned empty res');
           callback('empty res');
        }

        console.log('getLogin - result seems OK');

        //  test a matching password if the user is found we compare both passwords
        var userReceived = res.items[0].login;
        callback(null,userReceived);
        }
      });
},
function getPassword(userReceived, callback){
       console.log('getPassword');
      console.log(userReceived);
      callback(null,'done')
    }
], function(err){
  if (err) {
    console.error(err);
  }
    console.log('success');
});
}
Comments