javascript - MongooseMongoDB - How to use promise with aggregate queries - Stack Overflow

I am trying to perform 2 queries to 2 different collections in MongoDB via mongoose and then bine their

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])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745160313a4614359.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信