Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 828
gmaniac Avatar asked Sep 26 '22 11:09

gmaniac


1 Answers

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

like image 194
kentor Avatar answered Sep 30 '22 08:09

kentor