如何在函数外部提供变量的值

问题描述 投票:0回答:1

如何将变量设为全局变量?我想在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      
            }                
        })
node.js function scope global-variables var
1个回答
0
投票

您可以使用global.notes= []而不是写var notes = []。并将值推入循环。

global.notes.push(noteDoc.NoteText);

然后在循环迭代之后,您可以将值获取为

console.log(global.notes);
© www.soinside.com 2019 - 2024. All rights reserved.