question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pre remove middleware no longer called

See original GitHub issue

Do you want to request a feature or report a bug? Undocumented Behaviour, Possible Bug?

What is the current behavior? Using following code from previous version with the latest version:

let user = await Models.user.findById(req.params.id)
...
await user.remove()

When using Object.remove() it also calls schema.pre('remove', function... . But now in the latest version when using remove it prints: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.. But with that the pre(‘remove’) no longer gets called. I tried the deleteOne and the oder findByIdAndDelete() calls but the pre(‘remove’) gets never called.

What is the expected behavior? I can not find in the docs if the pre(‘remove’) still exists and if what action actually invoked it.

Please mention your node.js, mongoose and MongoDB version. node v9.5.0 mongoose 5.2.9 mongodb 3.6

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
lineuscommented, Aug 24, 2018

document.remove() has been updated in the master branch to call collection.deleteOne instead of collection.remove(). The hooks will still be called and no further deprecation warnings will come from calling document.remove(). The next release of mongoose will contain the fix for this 👍

0reactions
lobermanncommented, Aug 24, 2018

Awesome! Thank you very much for your assistance! Appreciate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"pre" and "post" remove Middleware not firing - Stack Overflow
Calling pre () or post() after compiling a model does not work in Mongoose in general. For example, the below pre('save') middleware will...
Read more >
Middleware pre('remove', ...) not getting called - Google Groups
This method sends a remove command directly to MongoDB, no Mongoose documents are involved. Because no Mongoose documents are involved, no middleware (hooks) ......
Read more >
Mongoose v6.8.1: Middleware
Pre middleware functions are executed one after another, when each middleware calls next . const schema = new Schema(..); schema.pre('save', ...
Read more >
MongoDB Pre Remove Middleware (065) - YouTube
DONATE TO THE SHOW!!! Donate any amount - https://www.paypal.com/cgi-bin/webscr... … Show more. Show more ...
Read more >
Mongoose Middleware | The Javascript
Mongoose calls document middleware functions with this a set to the ... When you call `doc.save()`, Mongoose calls your pre save middleware
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found