【发布时间】:2016-05-02 11:16:36
【问题描述】:
我试图在变量外部获取pend 变量的值并将其作为响应发送,但我将其作为undefined 获取。
app.post('/getData', function(req, res){
MongoClient.connect(url, function(err, db) {
for(i=0;i<5;i++)
{
var array;
var id = "4136354161ff135631";
var cursor =db.collection('messages').find( { "_id": ObjectId(id) } );
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
pend=doc;
}
});
if(i === 5(length)){ // -----> response is sent once the loop get over
console.log(pend);
res.send(pend);
db.close();
}
}
});
});
【问题讨论】:
标签: javascript node.js