代码之家  ›  专栏  ›  技术社区  ›  Atishay Jain

Mongoose中间件发布-“更新”不起作用

  •  0
  • Atishay Jain  · 技术社区  · 7 年前

    我用的是Mongoose4.11.3。我想用邮局 update 中间件。它不起作用。我试过了 save 一是工作正常。不知道怎么了。

    const mongoose = require('mongoose')
    const Schema = mongoose.Schema
    
    const ProfileSchema = Schema({
      // schema defined here
    })
    
    // working fine
    ProfileSchema.post('save', function () {
        console.log('save called') 
    })
    
    // not working
    ProfileSchema.post('update', function (err, doc, next) {
        console.log('update called') 
    })
    

    我打电话来 findOneAndUpdate ,数据正在更新,但更新中间件未被调用。即使pre也不起作用。

    事先谢谢。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Atishay Jain    7 年前

    这对我有用,但不知道为什么 'update' 不起作用。

    ProfileSchema.post('findOneAndUpdate', function () {
        console.log('pre - update')
    })
    
        2
  •  0
  •   Faheem    7 年前

    如果您使用

    Profile.updateOne();
    Profile.findOneAndUpdate();
    

    等等。

    为什么? findOneAndUpdate “正在工作,因为您正在使用

    Profile.findOneAndUpdate();
    

    应该是有道理的。