Legna
Legna

Reputation: 468

Is it possible to create a new hook after creating the model?

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?

Update

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

Answers (1)

Legna
Legna

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

Related Questions