【发布时间】:2016-08-11 00:30:38
【问题描述】:
每个人。我正在做一个在数据库中添加和更新文件的应用程序。 我的问题是,即使数据库更新调用仍未成功完成,我的循环仍在继续。 在继续循环之前,我如何设法使这些函数完成循环等待单元?
我们将不胜感激。
这是这段代码:
for(i = 0; i < treatments.length; i++) {
collection3.find({"_id": parseInt(treatments[i])}).toArray(function(err, results) {
if(results[0].maxPrice < parseInt(treatmentsPrices[i])){
collection3.update({"_id": parseInt(treatments[i])}, {"maxPrice": parseInt(treatmentsPrices[i])}, function(err, success) {
counter++;
console.log(err);
});
}
if(results[0].minPrice > parseInt(treatmentsPrices[i])){
collection3.update({"_id": parseInt(treatments[i])}, {"minPrice": parseInt(treatmentsPrices[i])}, function(err, success) {
console.log(err);
});
}
collection3.update({"_id": parseInt(treatments[i])}, {$addToSet: {"doctors": parseInt(doctorId)}}, function(err, success) {
console.log(err);
});
collection3.update({"_id": parseInt(treatments[i])}, {$addToSet: {"hospitals": parseInt(hospitals[i])}}, function(err, success) {
console.log(err);
});
if(newHospitalName != null) {
collection3.update({"_id": parseInt(treatments[i])}, {$addToSet: {"hospitals": parseInt(hospitalId)}}, function(err, success) {
console.log(err);
});
}
});
}
【问题讨论】:
标签: node.js mongodb for-loop asynchronous