DEV Community

Basit Raza
Basit Raza

Posted on

Answer: Using Mongoose `pre` hook to get document before findOneAndUpdate()

You can't retrieve the document in the pre hook.

According to the documentation pre is a query middleware and this refers to the query and not the document being updated.

Top comments (2)

Collapse
 
basit_raza profile image
Basit Raza

If we can't get the document in the pre hook then what is the purpose of pre hook?

Collapse
 
swapnilsoni1999 profile image
Swapnil Soni

There's a hack we can do is getQuery and we can use mongoose.model('modelName') and do find operation