Javascript 节点js中的两个异步循环和mysql查询

Javascript 节点js中的两个异步循环和mysql查询,javascript,node.js,Javascript,Node.js,我想按如下顺序打印我的信息: connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.

我想按如下顺序打印我的信息:

            connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid  FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.`status` = 1').then(function (rows) {
                async.each(rows, function (record, next) {
                    var count = 0;
                    ProcessRow(count, inventory, next);
                });
            });

            function ProcessRow(count, inventory, next)
            {
                if (inventory.length > 0) {
                    console.log(inventory[count].id); /// 4317648454 ... etc..
                    connection.query('UPDATE `transaction` SET `amount_two`= `amount_two` + 1 WHERE `id`= \'' + record.tid + '\'').then(function (err, res) {
                        console.log('3');
                        count++;
                        if(count < inventory.length)
                             ProcessRow(count, inventory, next);
                        else
                             next(); //assumeing you want to call next when you finish processing the inventory for this row.
                    });
                }
            }
这就是我到目前为止所做的:

connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid  FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.`status` = 1').then(function (rows) {
                    async.each(rows, function (record, next) {
                        async.each(inventory, function (rec, nex) {
                            connection.query('UPDATE `transaction` SET `amount_two`= `amount_two` + 1 WHERE `id`= \''+record.tid+'\'').then(function (err, res) {
                                console.log('3');
                            });
                            console.log(rec.id); /// 4317648454 ... etc..
                        });
                    });
                });
但出于某种奇怪的原因,它一直是这样:

            connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid  FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.`status` = 1').then(function (rows) {
                async.each(rows, function (record, next) {
                    var count = 0;
                    ProcessRow(count, inventory, next);
                });
            });

            function ProcessRow(count, inventory, next)
            {
                if (inventory.length > 0) {
                    console.log(inventory[count].id); /// 4317648454 ... etc..
                    connection.query('UPDATE `transaction` SET `amount_two`= `amount_two` + 1 WHERE `id`= \'' + record.tid + '\'').then(function (err, res) {
                        console.log('3');
                        count++;
                        if(count < inventory.length)
                             ProcessRow(count, inventory, next);
                        else
                             next(); //assumeing you want to call next when you finish processing the inventory for this row.
                    });
                }
            }

这似乎是因为您的connection.query看起来是异步的,您需要将资源清册从每个循环中分离出来,一次处理一个,如下所示:

            connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid  FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.`status` = 1').then(function (rows) {
                async.each(rows, function (record, next) {
                    var count = 0;
                    ProcessRow(count, inventory, next);
                });
            });

            function ProcessRow(count, inventory, next)
            {
                if (inventory.length > 0) {
                    console.log(inventory[count].id); /// 4317648454 ... etc..
                    connection.query('UPDATE `transaction` SET `amount_two`= `amount_two` + 1 WHERE `id`= \'' + record.tid + '\'').then(function (err, res) {
                        console.log('3');
                        count++;
                        if(count < inventory.length)
                             ProcessRow(count, inventory, next);
                        else
                             next(); //assumeing you want to call next when you finish processing the inventory for this row.
                    });
                }
            }
您可能也需要对“行”执行相同的操作? 希望这有帮助


编辑:我没有看到您在示例中调用next,因此我不确定您何时需要它启动,但我确实编辑了帖子,以反映您需要在行处理后调用它。

因此,嗯。。。下一个被调用的地方是哪里?假设他们想在库存循环中调用它,您可以传递它并在最后调用。我需要检查我的资料来源,15分钟后回到我的电脑时我会编辑。假设你正确地调用next,两种情况下的每个系列都会解决这个问题。你能举个例子吗?请