2
ClubSchema.post('deleteOne', {document : true, query : true}, function (doc) {
    console.log('Running');
    console.log(doc);
    let club_id = doc._id;
    console.log(club_id);
});

Output (Whenever the deleteOne() function is called) :

Running
{acknowledged: true, deletedCount: 1}
undefined

I tried all variations and tweaking with the function - with and without the options parameters, checking out the second parameter that's passed into the callback function, etc. - but to no avail. I even tried the this keyword, but again it's undefined as well.

The following was the callback function definition in the package files :

type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;

And the example in the documentation gives the following example :

schema.post('deleteOne', function(doc) {
  console.log('%s has been deleted', doc._id);
});

I tried replicating this same example, but the doc parameter passed into the function seems to be different from the one here.

Anything that I'm missing here ?

I have the same above post posted on github issues

  • Does this [answer](https://stackoverflow.com/a/62615433/21972629) help? – jQueeny Aug 17 '23 at 06:38
  • @jQueeny It's one solution to it, but it will be redundant if we have multiple deleteOne calls at multiple places. A way to implement this middleware is what I'm looking for. – amazinglySK Aug 17 '23 at 14:02

1 Answers1

1

The deleteOne,deleteMany returns the the database operations result, use removeOne or findOneAndDelete method instead if you want to return deleted document