Skip to content

Access document in the "deleteOne" post hook #10219

Answered by vkarpov15
vineetdigit asked this question in Q&A
Discussion options

You must be logged in to vote

It is expected, because by default deleteOne() is a hook on Query#deleteOne(), not Document#deleteOne(). And when you call Query#deleteOne(), the document being deleted may not be in memory at the time of being deleted.

I'd recommend doing MessageSchema.post('deleteOne', { document: true, query: false }, function() { this; /* deleted doc */ }) to register a hook for Document#deleteOne(), or use findOneAndDelete() instead.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vineetdigit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants