我有一个异步瀑布阵列,其中功能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:增加了整个事情,直到问题的功能
我没有看到你的代码中的任何async.waterfall功能。请张贴相关的代码也 –
编辑我的帖子,你需要遵循,以及的功能? – DetroitRead
函数签名是不正确的:'函数getIngrList(回调,用户ID)'应该是'功能getIngrList(用户ID,回调)' – taha