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 badges1 Answer
Reset to default 6var allDone = Promise.all(result1,result2)
should have been
var allDone = Promise.all([result1,result2])
本文标签: javascriptMongooseMongoDBHow to use promise with aggregate queriesStack Overflow
版权声明:本文标题:javascript - MongooseMongoDB - How to use promise with aggregate queries - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745160313a2645409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论