2014-12-13 34 views
0

我来了一个带有一些异步东西的裁剪器,我知道原因。当我运行async.serial时,我相信我的循环变量超出了范围。节点,迭代mysql行,使用异步库调用另一个mysql查询

该代码使用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 

感谢您的帮助球员

+0

你可能最好使用带'retdata'的'async.eachSeries()',而不是为'retdata'中的每个项目创建一个单独的函数。 – mscdex 2014-12-13 18:58:41

+0

感谢eachSeries和对回调名称的更改似乎已经成功了! 我已经改变它使用mapSeries作为变量作用域是目前的问题,我需要访问最终的变量。只需要弄清楚如何获取传递给mapSeries的变量。欢呼声 添加您的评论作为答案,我会将其标记为接受。 – BadWolf 2014-12-15 18:59:57

回答

0

你可能最好使用async.eachSeries()retdata而不是retdata创建的每个项目一个单独的函数。做出这一改变应该简化一点,应该比async.serial()更有效率。