2015-11-08 28 views
0

我在使用mongodb的node.js中编写web应用程序。我有这段代码,但不幸的是,它不按顺序执行,我的意思是for循环迭代比那些db函数执行更快,这使得混乱。如何使此代码以适当的顺序执行

hospitalsCollection.update({}, {$pull: {doctors: parseInt(id)}}, function(err, success) { 
    treatmentsCollection.update({}, {$pull: {doctors: parseInt(id)}}, function(err, success) { 
     hospitalsCollection.find({"_id": {$in: hospitalsIds}}).toArray(function(err, hospitalsList) { 
      for(i = 0; i < hospitalsList.length; i++) { 
       for(j = 0; j < hospitalsList[i].treatments.length; j++) { 
        var exists = false; 
        for(k = 0; k < hospitalsList[i].doctors.length; k++) { 
         doctorsCollection.find({"_id": parseInt(hospitalsList[i].doctors[k])}).toArray(function(err, doctorObj) { 
          for(l = 0 ; l < doctorObj.treatments.length; l++) { 
           if(doctorObj.treatments[l] == hospitalsList[i].treatments[j]) { 
            exists = true; 
            break; 
           } 
          } 
         }); 
         if(exists) 
          break; 
        } 
        if(exists) { 
         break; 
        } 
        else { 
         hospitalsCollection.update({"_id": parseInt(hospitalsList[i]._id)}, {$pull: {treatments: parseInt(hospitalsList[i].treatments[j])}}, function(err, success) { 
          treatmentsCollection.update({"_id": parseInt(hospitalsList[i].teratments[j])}, {$pull: {hospitals: parseInt(hospitalsList[i]._id)}}, function(err, success) { 
           console.log(err); 
          }); 
         }); 
        } 
       } 
      } 

      for(i = 0; i < treatments.length; i++) { 
       doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"},{$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":-1}}, {$limit:1} ], function(err, result) { 
        doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"},{$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":1}}, {$limit:1} ], function(err, result2) { 
         var maxPrice = result[0].treatments.price; 
         var minPrice = result2[0].treatments.price; 
         treatmentsCollection.update({"_id": parseInt(treatments[i])}, {$set: {"maxPrice": parseInt(maxPrice)}}, function(err, success) { 
          treatmentsCollection.update({"_id": parseInt(treatments[i])}, {$set: {"minPrice": parseInt(minPrice)}}, function(err, success) { 
           console.log(err); 
          }); 
         }); 
        }); 
       }); 
      } 
     }); 
    }); 
}); 

我真的不知道如何处理这个。任何帮助将不胜感激。谢谢。

回答

0

使用承诺(bluebird是伟大的)。并重构你的代码。

有了Promise,你可以做下面的事情。

dbUpdate1() 
.then(function(resultFromUpdate1) { 

    let multipleUpdates = [] 
    multipleUpdates.push(updateCollectionA()) 
    multipleUpdates.push(updateCollectionB()) 

    return Promise.all(multipleUpdates) 

.spread(afterUpdatingBothCollection) 


function updateCollectionA(argument) { 
    // body... 
} 

function updateCollectionB(argument) { 
    // body... 
} 

function afterUpdatingBothCollection(resultFrom1, resultFrom2) { 
    // body... 
} 
+0

我不太确定如何在我的情况下使用它;/ – kuba12

+0

@ kuba12 Promise允许您同步编写代码。 doThis()然后doThat()。我更新了上面的答案,使其更清晰 –

+0

好的,但我正在谈论那些for-loops。我不知道如何确保第8-15行在用'k'(第7行)或甚至'j'(第5行)迭代之前执行此循环。 – kuba12