我有这样的 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
。
我浪费了很多时间来调试它,但我想我犯了一些小错误,我不知道如何解决。