简体   繁体   English

Async.waterfall函数被调用两次

[英]Async.waterfall function gets called twice

I have an async waterfall Array where the function otherIngrLists() is the 3rd to be executed. 我有一个异步瀑布数组,其中函数otherIngrLists()是要执行的第三个函数。 Every function before that worked fine. 在此之前的每个功能都运行良好。

function otherIngrLists(userslist, callback){
    collection = db.get('ingrList');
    collection.find({"userid":{$ne:userid}},{},function(err,docs){ 
      if(!err){
        var otherLists = docs;
        var otherListsCount = docs.count();
        console.log(otherListsCount);
        callback(null, otherLists, otherListsCount, userslist);
      } else {
          callback(err, null);
      }
   });
 },

The Problem is that this function is called twice. 问题在于此函数被调用两次。 I assured this with a simple console.log(). 我用一个简单的console.log()保证了这一点。

How did I manage to call this function again? 如何设法再次调用此函数? Did I get the concept of callbacks wrong as I use them to be passed on to the next function? 我在使用回调传递给下一个函数时是否理解了回调的概念?

Also after this function executing twice an error ist thrown. 同样在此函数执行两次之后,将引发错误信息。 It has nothing to to with this problem though and I will concern my self with that later. 但是,这个问题无关紧要,以后我将关注自己。 Thank you for your time! 感谢您的时间!

Waterfall Array in router.get: router.get中的瀑布数组:

router.get('/:userid', function(req, res) {
  var db = req.db;
  var collection;
  var userid = req.params.userid;

  async.waterfall(
[
  function getIngrList(callback, userid) {
    var route = 'http://localhost:3000/users/zutatenliste/'+userid;

    request(route, function(err, response, body){
      if (!err && response.statusCode === 200) {
        var userlist = body;
        callback(null, userlist);
      } else {
        callback(err, null);
        return;
      }
    });
  },

  function otherIngrLists(userlist, callback){
    collection = db.get('zutatenListe');
    console.log(userid);
    collection.find({"userid":{$ne:userid}},{},function(err,docs){  
      if(!err){
        var otherLists = docs;
        var otherListsCount = docs.count();
        callback(null, otherLists, otherListsCount, userlist);
      } else {
        callback(err, null);
      }
    });
  },

  function pushInArray(otherLists, otherListsCount, userlist, callback){
    console.log("test");
    ...
    ...}
     }
   }

Edit 1: --Also either if cases are executed, first the true one then the false-- // Does not happen anymore 编辑1:-或者,如果案例被执行,则首先是真,然后是假-//不再发生

Edit 2: Added the whole Thing until the problematic function 编辑2:添加了整个东西,直到出现问题的功能

Please provide some Additional details as this function seems perfect and No, You haven't misunderstood the concept of callback you are using it correctly. 请提供一些其他详细信息,因为此功能似乎很完美,并且不能,您没有误解了正确使用回调的概念。

Structure of Async Waterfall 异步瀑布的结构

var create = function (req, res) {
        async.waterfall([
            _function1(req),
            _function2,
            _function3
        ], function (error, success) {
            if (error) { alert('Something is wrong!'); }
            return alert('Done!');
        });
    };

    function _function1 (req) {
        return function (callback) {
            var something = req.body;
            callback (null, something);
       }
    }

    function _function2 (something, callback) {
        return function (callback) {
           var somethingelse = function () { // do something here };
           callback (err, somethingelse);
        }
    }

    function _function3 (something, callback) {
        return function (callback) {
          var somethingmore = function () { // do something here };
          callback (err, somethingmore);
        }
   }

so, in waterfall you can pass the values to the next function and your 3rd function is correct.

Edited 编辑

  async.waterfall(
[
    //can not give userId as second parameter
  function getIngrList(callback) {
      //if you want userId you can pass as I shown above or directly use here if it's accessible
    var route = 'http://localhost:3000/users/zutatenliste/'+userid;

    request(route, function(err, response, body){
      if (!err && response.statusCode === 200) {
        var userlist = body;
        callback(null, userlist);
      } else {
        callback(err, null);
        // return; no need
      }
    });
  },

  function otherIngrLists(userlist, callback){
    collection = db.get('zutatenListe');
    console.log(userid);
    collection.find({"userid":{$ne:userid}},{},function(err,docs){  
      if(!err){
        var otherLists = docs;
        var otherListsCount = docs.count();
        callback(null, otherLists, otherListsCount, userlist);
      } else {
        callback(err, null);
      }
    });
  },

  function pushInArray(otherLists, otherListsCount, userlist, callback){
    console.log("test");
    ...
    ...}

As said you can not pass userId as last parameter over there. 如前所述,您不能将userId作为最后一个参数传递到那。 Let me know if you still get the same error. 让我知道您是否仍然遇到相同的错误。

First you need to declare you function: 首先,您需要声明您的功能:

function myFuntion(userId, callback) {
    async.waterfall([

        function(callback) {
            //do some thing here
            callback(null, userlist);
        }, function(userId, callback) {
            //do something here
            callback(null, orderList, orderListCount, userlist);
        }

    ], function(err, orderList, orderListCount, userlist) {
        if(err)
           console.log(err);
        else
           callback(orderList, orderList, userlist);
    })
}

After that you can use function: 之后,您可以使用功能:

myFuntion(userId, function(orderList, orderListCount, userlist) {

    console.log(orderList);
    console.log(orderListCount);
    console.log(userlist);
})

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

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