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
You have to define your hooks before calling mongoose.model()
Why isn't this on the docs?
Most helpful comment
You have to define your hooks before calling
mongoose.model()