【发布时间】:2014-05-12 18:07:06
【问题描述】:
使用 mongoose 从 db 和 Q 查询结果以获取承诺,但发现我很难仅仅获得可用用户列表。目前我有一些类似的东西:
var checkForPerson = function( person ) {
people = mongoose.model('Person', Person)
return people.findOne({"_id": person }, function(err, doc) {
if (err) console.log(err)
if (doc !== null) {
return doc
} else {
console.log('no results')
}
})
}
var promises = someArrayOfIds.map(checkForPerson);
// this is where I would like to have an array of models
var users = Q.all(promises)
//this fires off before the people.findOne query above to users is undefined
SomeOtherFunction( users )
如何在SomeOtherFunction 之前完成查询而不进行大量草率回调?
【问题讨论】:
标签: node.js mongodb mongoose promise q