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.
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