Reputation: 468
Elsewhere in the doc using mongoose.model ('MyModel').Schema
So for example, I have this code:
const UsuarioSchema = mongoose.model('MyModel').schema
UsuarioSchema.pre('save', ( next) => {
console.log('working')
next()
})
But is not inside of my_model.js
. It's possible to do this?
I found in the documentation that it is not possible to add a hook after compiling the model, which is what I was doing.
Define Middleware Before Compiling Models
Calling pre() or post() after compiling a model does not work in Mongoose in general. For example, the below pre('save') middleware will not fire.
Upvotes: 1
Views: 304
Reputation: 468
I found in the documentation that it is not possible to add a hook after compiling the model, which is what I was doing.
Define Middleware Before Compiling Models
Calling pre() or post() after compiling a model does not work in Mongoose in general. For example, the below pre('save') middleware will not fire.
Upvotes: 2