Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js .catch导致蓝鸟承诺的猫鼬代码出错_Node.js_Mongodb_Express_Mongoose_Bluebird - Fatal编程技术网

Node.js .catch导致蓝鸟承诺的猫鼬代码出错

Node.js .catch导致蓝鸟承诺的猫鼬代码出错,node.js,mongodb,express,mongoose,bluebird,Node.js,Mongodb,Express,Mongoose,Bluebird,我不知道为什么,但在我的bluebird promisified代码中包含一个.catch会抛出一个错误。我不是做错了,就是错过了什么 下面是一个典型的片段: Event.find({classId : req.params.id, active : true}) .then(function(events) { res.json({'events' : events}) }) 如果我改成 Event.find({classId : req.params.id,

我不知道为什么,但在我的bluebird promisified代码中包含一个
.catch
会抛出一个错误。我不是做错了,就是错过了什么

下面是一个典型的片段:

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错误

在我看来,这似乎是遵循蓝鸟文档,以及我能找到的任何代码示例

我做错了吗?以及
中的错误在哪里。查找
中的错误在哪里

如果有必要,启用bluebird promises的过程遵循熟悉的模式,并允许使用
。然后像champ一样使用
方法:

var mongoose = require('bluebird').promisifyAll(require('mongoose'));
这就是我创建mongoose实例的地方,该实例用于创建事件模型的模式



帖子回答:我已经开始使用
.findAsync
.findOneAsync
,现在我可以像预期的那样捕捉错误了。

catch
是承诺的非标准功能,它存在于
蓝鸟
承诺中,但不存在于实际的猫鼬
find
承诺中,至少在旧版本中是如此


在这种情况下,您希望使用
findAsync
而不是
find
catch
是承诺的非标准功能,它存在于
Bluebird
promisification中,但不存在于实际的猫鼬
find
承诺中,至少在旧版本中是如此


在这种情况下,您希望使用
findAsync
而不是
find

,正如耶肯所说的
catch
是承诺的非标准功能,但几乎存在于所有承诺实现中,包括
bluebird

您可以将猫鼬设置为使用
bluebird
promise,而不是其默认值

// Use bluebird
mongoose.Promise = require('bluebird');

您现在可以使用
catch

,正如Yerken所说的
catch
是承诺的非标准功能,但几乎存在于所有承诺实现中,包括
bluebird

您可以将猫鼬设置为使用
bluebird
promise,而不是其默认值

// Use bluebird
mongoose.Promise = require('bluebird');

您现在可以使用
catch

如果您不应该在Promisification后使用findAsync测试示例代码,正如您所提到的,它在我的测试中运行良好。也许是猫鼬版本和蓝鸟版本的问题?@zangw:你能给我你的版本让我验证一下吗?@Yerken:你能帮我解决这个问题吗?我认为。在mongoose中查找是异步的?@DanielDonaldson,mongoose版本,4.4.3。Bluebird 3.1.5版不应该在Promisification后使用findAsync测试示例代码正如您所提到的,它在我的测试中运行良好。也许是猫鼬版本和蓝鸟版本的问题?@zangw:你能给我你的版本让我验证一下吗?@Yerken:你能帮我解决这个问题吗?我认为。在mongoose中查找是异步的?@DanielDonaldson,mongoose版本,4.4.3。蓝鸟3.1.5版