2014-12-13 40 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()更有效率。