Node.js Mongoose中间件更新后不工作 未调用回调,但应按照mongoose中间件中的说明:
我尝试了预更新,但效果良好:Node.js Mongoose中间件更新后不工作 未调用回调,但应按照mongoose中间件中的说明:,node.js,mongoose,mongoose-middleware,Node.js,Mongoose,Mongoose Middleware,我尝试了预更新,但效果良好: 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
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',“这叫什么?”)代码>,这是预期的吗?
p、 s:
电脑:视窗10,,
mongoose版本:4.5.8现在看来,模式中应该只有一个参数。post
的回调函数表示已更新的文档。您的回调可能从未被钩子调用过,因为它从未提供过其余的参数。e、 g:
schema.post("update", function(doc) {
console.log('Update finished.');
});
而不是:
schema.post("update", function(error, res, next) {
console.log('Update finished.');
});
我会试试这个,但我的代码片段(带有3个参数的回调)实际上也是来自文档。很高兴这奏效了。看起来只有在出现错误时才会触发包含三个参数的回调。只有当你手动调用第二个函数(出于异步目的)时,第二个函数才起作用,而不管发生什么,第二个函数都会被激活。是的,我没有彻底阅读文档,这其实是不好的,但在某个时候,我仍然发现文档的某些部分有误导性
schema.post("update", function(error, res, next) {
console.log('Update finished.');
});