我有我的Node.js這樣的代碼Node.js的異步瀑布模塊回調
async.waterfall([
function(callback){
category = [{"id":1,"name":xxx},{"id":2,"name":yyy}];
callback(null,category);
},
function(category,callback){
var categoryData ={};
_.each(category,function(item){
categoryData[item.id] = item;
SolrClient.select(query,function(data){
//data is the response from SolrClient
categoryData[item.id]["data"] = data;
log(categoryData); //log1
},callback);
log(categoryData); //log2
});
log(categoryData); //log3
}
]);
log1 => the data that I have added to categoryData is logged correctly
log2 => I cant get the data that is logged in the callback function to SolrClient
log3 => same as log2
我明白,這事做可變範圍或 回調至SolrClient
不能訪問相同的categoryData
初始化之前_.each
。
我浪費了很多時間調試它,但我想我正在做一些小錯誤,我沒有得到如何解決。
真正能夠幫助你是正確的縮進。 –