node.js - Mongoose : Queries through an array, return a promise of an array -


i trying iterate through opts , make same query on each element, want in return promise of array containing results of queries. how can ?

i tried moment first item

//opts array of objects function getrecapofcampaign (campaignid, opts) {       var p_votes = models.bsvote     .find({       created: {         $gte: opts[0].fromdate,         $lt: opts[0].todate       }     })     .where('campaign').equals(campaignid)     .count()     .exec();    return p_votes; } 

there bunch of promise libs out there use or use native js promise implementation on newer releases of node. in particular promise.all method.

promise.all([array_or_promises]).then(values => {   console.log(values); // [array_of_results_from_promises] }); 

using promise.all along map should give want:

//opts array of objects function getrecapofcampaign(campaignid, opts) {   return promise.all(opts.map(function(opt) {     return models.bsvote.find({         created: {           $gte: opt.fromdate,           $lt: opt.todate         }       })       .where('campaign').equals(campaignid)       .count()       .exec();   })); } 

note: promise.all there no limit on number of promises concurrently running. if concern can either roll own or use 1 of many promise libs have methods this.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -