Home Mongoose cursor with async operations
Reply: 1

Mongoose cursor with async operations

Sandeep
1#
Sandeep Published in 2017-12-07 06:58:08Z

I am new to mongo and now drowned in whole lot of documentation.All I want is to use mongoose cursor to work async with my data operation inside cursor.It should wait to play next iteration until operation is complete. I read in a that we can do so by

  • QueryCursor#eachAsync(fn, [options], [options.parallel], [callback])

  • If fn returns a promise, will wait for the promise to resolve

Since i am new to mongo I don't know how to create a promise and resolve it later.

var cursor = Users.find({ "is_verified" : false }).cursor();
cursor.eachAsync(function (doc) { 
    //create promise here
    async.series({
       one: function(callback){
          callback(null,1);
       },
       two: function(callback){
          callback(null,2);
       },
       three: function(callback){
          callback(null,3);
       },
       four: function(callback){
          //resolve promise here
          if() callback(null,4)
       },
   },function(err,result){})


}).then(()=>console.log('FINISHED'));
israel.zinc
2#
israel.zinc Reply to 2017-12-07 07:51:46Z

I would call your function in an array like this: [This is semi pseudo-code]

for(parameter in parameters) {
  users.find({"parameter":parameter}, function( err, data) {
    if (err) return handleError(err);
    functionToFeedTemplate(data);
    functionToSendMail(data);
  }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31446 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO