MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

नोडज में, मोंगोडब कॉल रिटर्न तक फॉर लूप को कैसे रोकें

"async " एसिंक्रोनस लूपिंग को दूर करने और आपके कोड को पढ़ने/बनाए रखने में आसान बनाने के लिए एक बहुत लोकप्रिय मॉड्यूल है। उदाहरण के लिए:

var async = require('async');

function getHonorStudentsFrom(stuObjList, callback) {

    var honorStudents = [];

    // The 'async.forEach()' function will call 'iteratorFcn' for each element in
    // stuObjList, passing a student object as the first param and a callback
    // function as the second param. Run the callback to indicate that you're
    // done working with the current student object. Anything you pass to done()
    // is interpreted as an error. In that scenario, the iterating will stop and
    // the error will be passed to the 'doneIteratingFcn' function defined below.
    var iteratorFcn = function(stuObj, done) {

        // If the current student object doesn't have the 'honor_student' property
        // then move on to the next iteration.
        if( !stuObj.honor_student ) {
            done();
            return; // The return statement ensures that no further code in this
                    // function is executed after the call to done(). This allows
                    // us to avoid writing an 'else' block.
        }

        db.collection("students").findOne({'_id' : stuObj._id}, function(err, honorStudent)
        {
            if(err) {
                done(err);
                return;
            }

            honorStudents.push(honorStudent);
            done();
            return;
        });
    };

    var doneIteratingFcn = function(err) {
        // In your 'callback' implementation, check to see if err is null/undefined
        // to know if something went wrong.
        callback(err, honorStudents);
    };

    // iteratorFcn will be called for each element in stuObjList.
    async.forEach(stuObjList, iteratorFcn, doneIteratingFcn);
}

तो आप इसे इस तरह इस्तेमाल कर सकते हैं:

getHonorStudentsFrom(studentObjs, function(err, honorStudents) {
    if(err) {
      // Handle the error
      return;
    }

    // Do something with honroStudents
});

ध्यान दें कि .forEach() stuObjList में "समानांतर में" प्रत्येक तत्व के लिए आपके इटरेटर फ़ंक्शन को कॉल करेगा (यानी, यह एक इटरेटर फ़ंक्शन को अगले सरणी तत्व पर कॉल करने से पहले एक सरणी तत्व के लिए कॉल किए जाने की प्रतीक्षा नहीं करेगा)। इसका मतलब यह है कि आप वास्तव में उस क्रम की भविष्यवाणी नहीं कर सकते हैं जिसमें इटरेटर कार्य करता है - या इससे भी महत्वपूर्ण बात यह है कि डेटाबेस कॉल - चलेंगे। अंतिम परिणाम:सम्मान छात्रों का अप्रत्याशित क्रम। अगर आदेश मायने रखता है, तो .forEachSeries() का उपयोग करें समारोह।



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. NodeJS में नेवला स्थापित करना 0.10.8

  2. mgo क्वेरी से ObjectIdHex मान प्राप्त करें

  3. कर्ल --डेटा के साथ आरईएसटी मार्गों का परीक्षण, 404 लौटाता है

  4. MongoDB प्रतिकृति सेट के लिए SELinux को कैसे कॉन्फ़िगर करें

  5. geoNear गलत दूरी लौटाता है