【发布时间】:2016-05-25 19:57:24
【问题描述】:
我不知道为什么,但是在我的 bluebird 承诺代码中包含 .catch 会引发错误。我要么做错了,要么我错过了一些东西。
这是一个典型的 sn-p:
Event.find({classId : req.params.id, active : true})
.then(function(events) {
res.json({'events' : events})
})
如果我把它改成
Event.find({classId : req.params.id, active : true})
.then(function(events) {
res.json({'events' : events})
}).catch(function(err) {
// do something
})
然后我的测试,之前通过失败,500: server error。
在我看来,这似乎是遵循 bluebird 文档以及我能够找到的任何代码示例。
我做错了吗? .find 中的错误在哪里??
如果重要的话,启用 bluebird Promise 的过程遵循一个熟悉的模式,并允许像冠军一样使用 .then 方法:
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
这是我创建 mongoose 实例的地方,该实例用于为 Event 模型创建 Schema。
回复后:我已经开始使用.findAsync 和.findOneAsync,现在可以按预期捕获错误。
【问题讨论】:
-
在promisification后你不应该使用findAsync
-
如您所说的测试示例代码,它在我的测试中运行良好。可能是猫鼬版和蓝鸟版的问题?
-
@zangw : 你能给我你的版本让我验证吗?
-
@Yerken:你能帮我解决这个问题吗?我认为 .find 在 mongoose 中是异步的?
-
@DanielDonaldson,猫鼬版,4.4.3。蓝鸟版本 3.1.5
标签: node.js mongodb express mongoose bluebird