Bluebird — a promise was created in a handler but was not returned from it

橙三吉。 提交于 2019-12-08 00:13:58

问题


First of all, I know that I have to return promises to avoid this warning. I've also tried returning null as suggested here in the docs. Consider this piece of code, I'm using it in Mongoose's pre-save hook, but I've experienced this warning in other places:

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
});

I've also tried (initially) this way:

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

But it doesn't work either. Oh, and I have to mention, that I use Bluebird:

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

mongoose.Promise = Promise;

Not a duplicate of A promise was created in a handler but was not returned from it, the guy forgot to return a promise.


回答1:


The problem is pretty much using a next callback at all, which calls functions that create promises without returning them. Ideally the hooks just needed to return promises instead of taking callbacks.

You should be able to prevent the warning by using

.then(function(result) {
    next(null, result);
    return null;
}, function(error) {
    next(error);
    return null;
});


来源:https://stackoverflow.com/questions/37861736/bluebird-a-promise-was-created-in-a-handler-but-was-not-returned-from-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!