Search code examples
node.jsmongodbmongoose

mongoose pre update not firing


I have follow the directions in mongoose here

PostSchema.pre('update', function() {
    console.log('pre update');
    console.log(this);
});

it is not firing this middleware. Am I missing something here?

I have added next so it looks exactly like my pre save, however that still does nothing.


Solution

  • Make sure you don't define this after mongoose.model() has been called. Please also take note that findOneAndUpdate / upserts or updates won't trigger this hook. Another reason why it wouldn't execute is that validation fails. Therefore you would need to setup a pre('validate') hoke