Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Async.瀑布不传递嵌套mysql查询的回调?_Javascript_Node.js_Asynchronous_Node Mysql - Fatal编程技术网

Javascript Async.瀑布不传递嵌套mysql查询的回调?

Javascript Async.瀑布不传递嵌套mysql查询的回调?,javascript,node.js,asynchronous,node-mysql,Javascript,Node.js,Asynchronous,Node Mysql,我有一系列嵌套的mysql查询需要串联执行,因为后面的查询依赖于前面的查询的结果——async.瀑布似乎是完美的解决方案。但是,瀑布的第二步是无法将其结果附加到我的数组中: async.waterfall([ function(callback) { connection.query(query, function(err, rows, fields) { if (err) throw err; var top_ten_publishers = [];

我有一系列嵌套的mysql查询需要串联执行,因为后面的查询依赖于前面的查询的结果——async.瀑布似乎是完美的解决方案。但是,瀑布的第二步是无法将其结果附加到我的数组中:

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,我会看到新值被正确地预先添加到数组中,但是当最后一步运行时,新值不在数组中。我是否在第二步中执行了异步操作,以便在运行查询之前调用回调?

在第二个函数中
前十名发布者
forEach
在迭代完成后,在每次迭代中进行异步调用,然后函数退出。由于无法保证对
async.query
的异步调用也能完成,因此最终会得到混乱的结果

请尝试此修改版本的
十大出版商

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

你说得绝对正确——我发誓我确实试过了,但效果非常好。谢谢