Access document in the "deleteOne" post hook #10219
-
I have defined a post hook on the MessageSchema. And I was trying to access the document that was deleted. The hook gets called after a successful deletion, but I am not able to use the document that was deleted in the post hook. Is following behavior expected?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It is expected, because by default I'd recommend doing |
Beta Was this translation helpful? Give feedback.
It is expected, because by default
deleteOne()
is a hook onQuery#deleteOne()
, notDocument#deleteOne()
. And when you callQuery#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 forDocument#deleteOne()
, or usefindOneAndDelete()
instead.