如何完成如何使用Node.js和MongoDB回调执行后,执行新的功能? [重复]
这个问题已经在这里有一个答案:
- How do I return the response from an asynchronous call? 36个回答
findOneAndUpdate
异步运行,因此当循环结束后,功能将不会做他们的工作呢。
我建议你使用async / await
这样你就可以轻松地运行两个步骤同步(第一个数据库的更新,然后调用在年底的功能):
var updateArr=[];
TempCollection.find({}, async function(err, docs) {
for(var i = 0; i < docs.length; i++) {
if (docs[i]['login_id'] === '' || docs[i]['login_id'] == null) {
var zon = docs[i]['zone'] + '@oditeksolutions'
try {
var upd = await TempCollection.findOneAndUpdate({ $or: [{ login_id: null }, { login_id: '' }]}, { $set: { login_id: zon }}, { new: true }).exec()
updateArr.push(upd)
} catch(err1) {
console.log(err1) // throw err1
}
}
}
console.log('lrn::',updateArr.length);
if (updateArr.length > 0) {
createAllocation();
createUser();
}
})
评论列表(0)
- 暂无评论