【发布时间】:2015-12-03 11:08:30
【问题描述】:
我正在使用带有 Bluebird promisifyall 的猫鼬,如下所示:
var mongoose = require('bluebird').promisifyAll(require('mongoose'))
我想使用 where 从 mongo 中检索文档,如下所示:
// Gets a list of Posts
exports.index = function(req, res) {
console.log(req.query);
Post.findAsync()
.whereAsync({author: req.query.id})
.execAsync()
.then(function(entity) {
if (entity) {
res.status(statusCode).json({
status_code: statusCode,
data: entity
});
}
})
.catch(function(err) {
res.status(200).json({
status_code: statusCode,
message: 'error occured',
errors: err
});
});
};
但它只是挂起,我做错了什么? 将不胜感激使用蓝鸟和猫鼬的 promisifyall 的任何帮助,谢谢:)
【问题讨论】:
-
Mongoose 已经原生地返回了一个 Promise,而不需要拉入 bluebird Promise。如前所述,唯一获得“承诺”的方法是异步方法,如
.findOne()到.findOneAsync(),而不是非异步的“助手”,如.where()。简而言之,除非您特别想要“添加”,例如.spread(),否则您不需要 bluebird 承诺
标签: javascript mongodb mongoose promise bluebird