蓝鸟 - 在处理程序中创建了一个诺言,但没有从中归还



首先,我知道我必须 return承诺避免此警告。我还尝试过如文档中建议的那样返回null。考虑一下这件代码,我正在Mongoose的保存前挂钩中使用它,但是我在其他地方经历了此警告:

var Story = mongoose.model('Story', StorySchema);
StorySchema.pre('save', function(next) {
    var story = this;
    // Fetch all stories before save to automatically assign
    // some variable, avoiding conflict with other stories
    return Story.find().then(function(stories) {
        // Some code, then set story.somevar value
        story.somevar = somevar;
        return null;
    }).then(next).catch(next); // <-- this line throws warning
});

我也尝试过(最初)以这种方式:

        story.somevar = somevar;
        return next(); // <-- this line throws warning
    }).catch(next);

,但也行不通。哦,我不得不提到,我使用蓝鸟:

var Promise = require('bluebird'),
    mongoose = require('mongoose');
mongoose.Promise = Promise;

在处理程序中创造了诺言的重复,但没有从中归还,那家伙忘了返回诺言。

这个问题几乎使用了next回调,该回调调用函数创建承诺而无需返回。理想情况下,挂钩只需要返回承诺而不是进行回调。

您应该能够通过使用

来防止警告
.then(function(result) {
    next(null, result);
    return null;
}, function(error) {
    next(error);
    return null;
});

最新更新