mongoose-middleware

pre save middleware in mongoose

左心房为你撑大大i 提交于 2021-01-29 16:20:31
问题 i am first time using pre save middleware and getting a bit confusion in it. It runs perfectly fine and also my save method is getting executed eventhough i am not calling the next() case 1 tourSchema.pre('save', function () { console.log('first middleware is getting called'); }) But when i do like this when next is declared inside the function params but i don't call the next() it hangs there and the save method is not getting executed case 2 tourSchema.pre('save', function (next) { console

Mongoose middleware post update not working

£可爱£侵袭症+ 提交于 2019-12-11 06:38:16
问题 Callback is not being called, but it should as per documented in mongoose middleware: schema.post('update', function(error, res, next) { if (error.name === 'MongoError' && error.code === 11000) { next(new Error('There was a duplicate key error')); } else { next(error); } }); I tried pre update and it works: schema.pre("update", function(next) { console.warn('results', "i am called"); next(new Error("error line called")); }); But what I wanted is post update: schema.post("update", function