Node.js Mongooe更新前/更新后挂钩

Node.js Mongooe更新前/更新后挂钩,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,我有一个带有“status”字段的“User”模式,我想跟踪此字段的更改,因此,当用户将status字段更改为“locked”时,将执行作业 用猫鼬怎么做 我希望听到“status”字段中的更新更改,如果状态更新为“locked”,我希望调用第三方API阻止用户访问应用程序 我尝试了以下方法 const usersSchema = new mongoose.Schema({ status: {type: String, default: 'active', required: true, e

我有一个带有“status”字段的“User”模式,我想跟踪此字段的更改,因此,当用户将status字段更改为“locked”时,将执行作业

用猫鼬怎么做

我希望听到“status”字段中的更新更改,如果状态更新为“locked”,我希望调用第三方API阻止用户访问应用程序

我尝试了以下方法

const usersSchema = new mongoose.Schema({
  status: {type: String, default: 'active', required: true, enum: ['active', 'locked']},
}, {timestamps: {createdAt: 'createdDate', updatedAt: 'modifedDate'}});

usersSchema.pre('updateOne', function(next) {
  console.log('pre save');
  if (this.getUpdate().status) {
    // Run job ie call an third pary api to block the user from the application 
    console.log('do a job');
  }
  next();
});

为什么不在更新user.status的路由中执行此操作?我想知道除了在路由中检查它之外,还有什么更好的方法。就在db级别执行此操作而言,您已经尝试过的是方法。你有什么错误吗?