gpt4 book ai didi

mysql - Node ,迭代 mysql 行,使用异步库调用另一个 mysql 查询

转载 作者:行者123 更新时间:2023-11-29 23:19:24 25 4
gpt4 key购买 nike

我来了一些异步的东西,我知道原因。我相信当我运行 async.serial 时,我的循环变量超出了范围。

该代码使用 mysql 模块迭代一个数据库中的一系列行,并根据某个值调用另一个查询并将结果附加到文档中。我的基本代码是

//data access
LoadData: function(query, callback) {
//mysql connection stuff
connection.connect();
var query = connection.query(sp, function(err, rows, fields){
if (err) console.log(err);
connection.end();
callback(rows);
});
});
// this works fine, my call back is fired without any problems

//controller
//load data from mysql and proceed when the callback has been called
LoadData(sp, function (retdata) {
var tasks = [];
for i=0;i<retdata.length;i++)
{
tasks.push(function(callback){
LoadData(retdata[i], function (ret) {
//each row from the original results will call a new store proc, and append the results to a file
//other stuff
}
callback();
})
}
});

async.serial(tasks,function(){...})
//there should be 3 unique rows from the first data set, however the same data is written to file 3 times - so it's a variable issue, just not sure how best to tackle this - I'm using to C# lol
//I tried the async foreachserial() that didn't work either

感谢大家的帮助

最佳答案

您最好将 async.eachSeries()retdata 一起使用,而不是为 retdata 中的每个项目创建单独的函数。进行此更改应该会稍微简化一些事情,并且应该比 async.serial() 更高效。

关于mysql - Node ,迭代 mysql 行,使用异步库调用另一个 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27461234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com