我在异步查询中遇到了很多问题
我必须创建一个对象运行时,我尝试使用异步 我的剧本:
async.forEach(myArray, function (item, callback){
retrieveUser(username,item,function(err,item){
console.log("Query");
myobj.foo = item; //popolate the object
callback(); // tell async that the iterator has completed
});
}, function(err) {
console.log('iterating done');
});
我的结果是
Query
Query
iterating done
Query
iterating done
Query
iterating done
Query
iterating done
Query
iterating done
Query
iterating done
Query
并且我的对象未正确填充
答案 0 :(得分:0)
您使用了相同的变量名称item
。更新您的功能,如下所示;
async.forEach(myArray, function (data, callback){
retrieveUser(username,data,function(err,item){
console.log("Query");
myobj.foo = item; //popolate the object
callback(); // tell async that the iterator has completed
});
}, function(err) {
console.log('iterating done');
});