Domanda

mydocuments.find({}).toArray returns empty in my code below. I could see couple of solutions posted but not apply them since I am using MongoClient.connect . Any assistance will be much appreciated.

var MONGOHQ_URL="mongodb://harishvc:supersecretreally@something.com:12345/abcd";
 var mongodb = require('mongodb');
 MongoClient = mongodb.MongoClient;
 var async = require('async');
 ….
  async.series([
  function (callback) {
   console.log("start: db connection");
      MongoClient.connect(MONGOHQ_URL, { server: { auto_reconnect: true } }, function(err, db2) {
         if (err) {return callback(err, "connect error");}
         db = db2;
         console.log("end: db connection");
         callback(null,"end: db connection");
     });
  },
  function (callback) {
      console.log("start: getting handle to collection");
      mydocuments = db.collection('test');  
      console.log("end: getting handle to collection");
      callback(null,"end: getting handle to collection");
  },
  function (callback) {
     console.log ("start: inserting new entries ....");
     mydocuments.insert({i:1},callback);    
     console.log ("end: inserting new entries ....");
     callback(null,"end: inserting new entries");
   },
   function (callback) {
      console.log("start: listing all entries ....");  
      mydocuments.find({}).toArray(function(err, docs) {
          if (err) {return callback(err, "connect error");}
           // Does not get executed???
            console.log("Hello World!");
      });       
      console.log("end: listing all entries ....");
      callback(null,"end: listing all entries");    
  }],
      …..

Output

start: db connection
end: db connection
start: getting handle to collection
end: getting handle to collection
start: inserting new entries ....
end: inserting new entries ....
start: listing all entries ....
end: listing all entries ....
È stato utile?

Soluzione

The problem is in the 3rd function where the data is inserted. Although you pass in the callback, this insert is asynchronous, so it continues to execute the next 2 lines of code below that which are:

console.log ("end: inserting new entries ....");
callback(null,"end: inserting new entries");

This 2nd callback() call in this 3rd function tells async to go ahead and continue with the 4th function which does the find(). At this point, your insert from the 3rd function might not be completed yet. That's why the result of the find in the 4th function is empty.

Your 3rd function should look like this:

 function (callback) {
     console.log ("start: inserting new entries ....");
     mydocuments.insert({i:1},function(err) {
         if (err) {
             callback(err, "error inserting");
         } else {
             callback(null,"end: inserting new entries");
         }
         console.log ("end: inserting new entries ....");

     });    
 },

This guarantees that your insert in the 3rd function is completed before going on to the next function (4th) which is doing a find().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top