如何将变量设为全局变量?我想在async.eachSeries函数之外使用notes变量的值。
customerModel.findOne({_id:customer_Id},function(err,custDoc){
if(err){console.log("***Error::"+err);}
else{
var notes = [];
async.eachSeries(custDoc.CustNotesRef,function(noteRef,cb){
notesModel.findOne({_id:noteRef},function(err,noteDoc){
if (err) {
console.log("error at finding vendors");
cb();
} else if(noteDoc == null || noteDoc == "") {
cb();
} else {
notes.push(noteDoc.NoteText);
cb();
}
})
},function (err) {
if (err) {
console.log(err);
} else {
console.log("Notes::"+notes); // value of notes is coming to here
}
})
console.log("Notes::"+notes); // here notes value is undefined
}
})
您可以使用global.notes= []
而不是写var notes = []
。并将值推入循环。
global.notes.push(noteDoc.NoteText);
然后在循环迭代之后,您可以将值获取为
console.log(global.notes);