【发布时间】:2015-09-18 15:04:24
【问题描述】:
我有一系列嵌套的 mysql 查询,我需要连续执行,因为后面的查询依赖于早期查询的结果 - async.waterfall 似乎是完美的解决方案。但是,瀑布的第二步未能将其结果附加到我的数组中:
async.waterfall([
function(callback) {
connection.query(query, function(err, rows, fields) {
if (err) throw err;
var top_ten_publishers = [];
rows.forEach(function (result) {
var publisher_row = [result.name, result.sale_amount, result.actual_commission, result.transactions, result.post_date, result.toolbar_id, result.shop_source];
top_ten_publishers.push(publisher_row);
})
callback(null, top_ten_publishers);
})
},
function(top_ten_publishers, callback) {
top_ten_publishers.forEach(function (publisher_row) {
connection.query(“select sum(sale_amount) as 'sale_amount', sum(actual_commission) as 'actual_commission', count(*) as transactions, from table where mall_name = '" + publisher_row[0] + "' and fc_post_date between '" + start_date_wow + "' and '" + end_date_wow + "' Group by name order by sum(sale_amount) desc;", function (err, rows, fields) {
rows.forEach(function (result) {
var wow = (publisher_row[3] - result.sale_amount) / result.sale_amount;
publisher_row.unshift(wow);
})
});
})
callback(null, top_ten_publishers);
}
], function (err, result) {
console.log(result);
});
如果我将 console.log 放在瀑布的第二步中,我会看到新值正确地添加到数组中,但是当最后一步运行时,新值不在数组中。我是否在第二步中做一些异步操作,让回调在查询运行之前被调用?
【问题讨论】:
标签: javascript node.js asynchronous node-mysql