【发布时间】:2017-10-07 10:13:00
【问题描述】:
我有三个操作要一个接一个地做
1.从db中获取一些行
2.forloop中的另一个mysql查询,用于获取一些数据并存储在变量中
3.显示数据
为此,我正在使用async waterfall 方法。
async.waterfall([
function(callback){
//first sql and pass the result to second function
collection.getAllCollections(function (status,error,result) {
callback(null,result);
});
},
//running another query in loop with result with previous
function(result, callback){
for(var i=0;i<result.length;i++){
collection.getImages(result[i].id,function (status,error,user) {
//append the query result to old result
result[i]['users'] = user;
});
}
callback(null,result);
}
], function (err, result) {
console.log("result",result);
});
但问题最终result 不包含user 结果,因为第二个查询(for 循环中的查询是异步的)
【问题讨论】:
标签: mysql node.js express asynchronous