Mongoose: Pre and Post middleware not firing at all

Created on 20 Mar 2017  路  2Comments  路  Source: Automattic/mongoose

What is the current behavior?
The code below should trigger the pre and post hooks.

Asset.findById(assetId).then(asset => asset.remove())

AssetSchema.js

const AssetSchema = mongoose.Schema({
  name: { type: String, required: true, trim: true }
});

const Asset = mongoose.model('Asset', AssetSchema);

AssetSchema.pre('remove', function(next) {
  console.log('pre', this._id)
})

AssetSchema.post('remove', function(next) {
  console.log('post', this._id)
})

MongoDB: 3.4.0
Mongoose: 4.9.1
NodeJS: 6.8.1

docs

Most helpful comment

You have to define your hooks before calling mongoose.model()

All 2 comments

You have to define your hooks before calling mongoose.model()

Why isn't this on the docs?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcolens picture dcolens  路  44Comments

saudelog picture saudelog  路  79Comments

cosminn777 picture cosminn777  路  158Comments

bendytree picture bendytree  路  53Comments

fundon picture fundon  路  42Comments