猫鼬中间件预更新 [英] mongoose middleware pre update

查看:61
本文介绍了猫鼬中间件预更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

schema.pre('save', function (next) {
  if (this.isModified('field')) {
    //do something
  }
});

但是我现在需要在schema.pre('update'挂钩中使用相同的功能isModified,但是它不存在.有谁知道我如何在update挂钩中使用相同的功能?

but I now need to use this same function isModified in a schema.pre('update' hook, but it does not exists. Does anyone know how I can use this same functionality in the update hook?

推荐答案

根据:

查询中间件与文档中间件的区别很细微,但 重要方式:在文档中间件中,这是指文档 正在更新.在查询中间件中,猫鼬不一定具有 对要更新的文档的引用,因此这是指查询 对象而不是要更新的文档.

Query middleware differs from document middleware in a subtle but important way: in document middleware, this refers to the document being updated. In query middleware, mongoose doesn't necessarily have a reference to the document being updated, so this refers to the query object rather than the document being updated.

update是查询中间件,this引用没有isModified方法的查询对象.

update is query middleware and this refers to a query object which has no isModified method.

这篇关于猫鼬中间件预更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆