简体   繁体   English

异步watefall不会调用函数

[英]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 : 所以我实际上在用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()我告诉该函数等待,直到它收到所有的回调返回,所以我不明白为什么connect变量是'undefined'吗?

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. 我看到的是,如果两个“ if”语句都返回false,则getLogin函数中缺少的是回调。 in this case you'll get stuck in the first function and you won't advance to 'getPassword' function. 在这种情况下,您将陷入第一个功能,而不会前进至“ 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');
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM