2017-07-15 130 views
0

我有一个异步瀑布阵列,其中功能otherIngrLists()是要被执行的第三。在此之前的每个功能都很好。Async.waterfall函数被调用两次

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

问题是这个函数被调用两次。我用一个简单的console.log()来保证这一点。

我是如何设法再次调用这个函数?当我使用它们传递到下一个函数时,我是否遇到了回调错误的概念?

而且该功能后执行两次错误IST抛出。尽管这个问题没有任何问题,但我会在稍后关注我的自我。 谢谢你的时间!

瀑布数组中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"); 
    ... 
    ...} 
    } 
    } 

编辑1:要么 - 兼论如果案件被执行,首先是真正的一个,则false-- //不再发生

编辑2:增加了整个事情,直到问题的功能

+0

我没有看到你的代码中的任何async.waterfall功能。请张贴相关的代码也 –

+0

编辑我的帖子,你需要遵循,以及的功能? – DetroitRead

+0

函数签名是不正确的:'函数getIngrList(回调,用户ID)'应该是'功能getIngrList(用户ID,回调)' – taha

回答

0

请提供一些额外的细节,因为这功能似乎完全没有,你有没有误解你使用了正确的回调的概念。异步瀑布

结构

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. 

编辑

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"); 
    ... 
    ...} 

至于说你不能在那里通过用户ID作为最后一个参数。让我知道你是否仍然有同样的错误。

+0

谢谢。但是我忘了添加包含async.waterfall的get-request。那就是我有用户名的地方。我的问题完全没有提出,我很抱歉 – DetroitRead

0

首先,你需要声明你的功能:

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

后,您可以使用函数:

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

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