【发布时间】:2016-10-18 02:54:21
【问题描述】:
首先,我知道我必须return 承诺避免这个警告。我也尝试按照here in the docs 的建议返回null。考虑这段代码,我在 Mongoose 的 pre-save 钩子中使用它,但我在其他地方遇到过这个警告:
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);
但它也不起作用。哦,我不得不提一下,我使用的是 Bluebird:
var Promise = require('bluebird'),
mongoose = require('mongoose');
mongoose.Promise = Promise;
不是A promise was created in a handler but was not returned from it的复制品,这家伙忘了返回一个承诺。
【问题讨论】:
标签: javascript node.js mongoose promise bluebird