Bluebird — 在处理程序中创建了一个承诺,但没有从中返回
Bluebird — a promise was created in a handler but was not returned from it
首先,我知道我必须return
承诺避免此警告。我也按照建议 here in the docs 尝试 returning 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);
但是也没用。哦,我不得不提一下,我使用 Bluebird:
var Promise = require('bluebird'),
mongoose = require('mongoose');
mongoose.Promise = Promise;
不是 的重复,这家伙忘了 return 一个承诺。
问题几乎完全是在使用 next
回调,它调用了创建承诺的函数,但没有 return 调用它们。理想情况下,钩子只需要 return 承诺而不是接受回调。
您应该可以通过使用
来阻止警告
.then(function(result) {
next(null, result);
return null;
}, function(error) {
next(error);
return null;
});
首先,我知道我必须return
承诺避免此警告。我也按照建议 here in the docs 尝试 returning 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);
但是也没用。哦,我不得不提一下,我使用 Bluebird:
var Promise = require('bluebird'),
mongoose = require('mongoose');
mongoose.Promise = Promise;
不是
问题几乎完全是在使用 next
回调,它调用了创建承诺的函数,但没有 return 调用它们。理想情况下,钩子只需要 return 承诺而不是接受回调。
您应该可以通过使用
来阻止警告.then(function(result) {
next(null, result);
return null;
}, function(error) {
next(error);
return null;
});