admin管理员组

文章数量:1415655

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6
var allDone = Promise.all(result1,result2) 

should have been

var allDone = Promise.all([result1,result2])

本文标签: javascriptMongooseMongoDBHow to use promise with aggregate queriesStack Overflow