回调未被调用,但应该按照 mongoose 中间件中的记录:
schema.post('update', function(error, res, next) {
if (error.name === 'MongoError' && error.code === 11000) {
next(new Error('There was a duplicate key error'));
} else {
next(error);
}
});
我尝试了预更新,它有效:
schema.pre("update", function(next) {
console.warn('results', "i am called");
next(new Error("error line called"));
});
但我想要的是更新后:
schema.post("update", function(error, res, next) {
console.warn('results', "is this called?");
});
实际模型更新:
MyModel.update({_id : 123}, req.payload, function (err, numberAffected, rawResponse) {
reply("done!");
});
我没有看到日志 console.warn('results', "is this called?");
,这是预期的吗?
附: 机器:Windows 10, Mongoose 版本:4.5.8
最佳答案
访问the docs ,看起来您应该在 schema.post
的回调函数中只包含一个代表已更新文档的参数。 Hook 可能永远不会调用您的回调,因为它从未提供其余参数。例如:
schema.post("update", function(doc) {
console.log('Update finished.');
});
而不是:
schema.post("update", function(error, res, next) {
console.log('Update finished.');
});
关于node.js - Mongoose 中间件更新后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41755806/