异步watefall不调用函数

时间:2023-02-06 02:32:57

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

所以我实际上正在使用node.Js来处理一个简单的程序,并且我使用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).

    使用node-inspector我发现主要问题(我认为)是当它进入瀑布函数时它不执行findOne的回调函数它会直接跳过它并直接跳转到getPassword函数(不执行)太)。

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

EDIT: After adding the different missing cases of tests(which was why the callback didn't worked) I have this connection function:

编辑:添加不同的测试缺失案例(这就是为什么回调没有工作)我有这个连接功能:

exports.connection = function (login,password) {

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

    console.log('login received :',userReceived.login);
    var Ulogin = userReceived.login;
    var Upassword = userReceived.password;
 // function that compare the received password with the encrypted 
 //one
    bcrypt.compare(password, Upassword, function(err, isMatch) {
        if (err) {
          console.log(err);
          callback(err,null);
          return;
        }
        else if (isMatch) {
          console.log('Match', isMatch);
          callback(null,isMatch);
        }
        else {
          console.log('the password dont match', isMatch);
          callback('pwd error',null);
        }
    });
},
], function(err){
  if (err) {
    console.error('unexpected error while connecting', err);
    return false;
  }
    console.log('connected successfully');
    return true;
});
}

And in my main file server.js i'm doing currently doing :

在我的主文件server.js中我正在做的事情:

var connect = users.connection(login,password);
//the goal is to use the connect variable to know if the connection
//failed or not but it's 'undefined'
if(connect){
       res.send('youyou connecté');
 }
 else {
       res.send('youyou problem');
 }

this absolutely don't work so i tried to use Q library but I have an error saying

这绝对不行,所以我试图使用Q库,但我有一个错误说

"TypeError: Cannot read property 'apply' of undefined at Promise.apply"

“TypeError:无法读取Promise.apply中未定义的属性'apply'”

here is the code using Q:

这是使用Q的代码:

  app.post('/signup', function (req, res) {
  var login = req.body.login;
   var password = req.body.password;
   Q.fcall(users.connection(login,password))
   .then(function (connect) {
     if(connect){
       res.send('connected');
     }
     else {
       res.send('problem');
     }
   })
   .catch(function (error) {
       throw error;
   })
   .done();
});

but i am a little bit astonished i thought that by using async.waterfall() i told the function to wait until it received all the callbacks return so i don't understand why the connect variable is 'undefined'?

但我有点惊讶我认为通过使用async.waterfall()我告诉函数等待它收到所有的回调返回所以我不明白为什么连接变量是'undefined'?

1 个解决方案

#1


0  

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

我不明白的是 - 流量究竟是什么? 'usersModel.findOne'被调用了吗?

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.

我看到getLogin函数中缺少的是在'if'语句返回false的情况下的回调。在这种情况下,你将陷入第一个功能,你不会进入'getPassword'功能。

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

#1


0  

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

我不明白的是 - 流量究竟是什么? 'usersModel.findOne'被调用了吗?

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.

我看到getLogin函数中缺少的是在'if'语句返回false的情况下的回调。在这种情况下,你将陷入第一个功能,你不会进入'getPassword'功能。

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