I am trying to perform 2 queries to 2 different collections in MongoDB via mongoose and then bine their results for a REST API response.
Example:
var result1 = Model1.aggregate([<operations here>]).exec()
var result2 = Model2.aggregate([<operations here>]).exec()
var allDone = Promise.all(result1,result2)
allDone.then(function(data1,data2){
//Do something with both data
})
I get this error TypeError: Cannot read property 'readPreference' of undefined
Which used to happen when the function signature for the callback wasnt function(err,docs){...
If I use callbacks for Aggregators , it works but I didn't want to chain callbacks/the queries and thought this way would be more efficient.
I found this Mongoose aggregate cursor promise
But wanted to know if this is possible with native promises in a simpler way. I do not want to iterate through the cursor too as explained in the above SO answer.
I am trying to perform 2 queries to 2 different collections in MongoDB via mongoose and then bine their results for a REST API response.
Example:
var result1 = Model1.aggregate([<operations here>]).exec()
var result2 = Model2.aggregate([<operations here>]).exec()
var allDone = Promise.all(result1,result2)
allDone.then(function(data1,data2){
//Do something with both data
})
I get this error TypeError: Cannot read property 'readPreference' of undefined
Which used to happen when the function signature for the callback wasnt function(err,docs){...
If I use callbacks for Aggregators , it works but I didn't want to chain callbacks/the queries and thought this way would be more efficient.
I found this Mongoose aggregate cursor promise
But wanted to know if this is possible with native promises in a simpler way. I do not want to iterate through the cursor too as explained in the above SO answer.
Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked May 19, 2017 at 7:40 onkknoonkkno 7096 silver badges18 bronze badges1 Answer
Reset to default 6var allDone = Promise.all(result1,result2)
should have been
var allDone = Promise.all([result1,result2])
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745160313a4614359.html
评论列表(0条)