I've searched this question on here, but as a beginner, the answers are a little complex and go over my head a bit. I'm using bcrypt to hash passwords and have set up my schema to allow me to use middleware to see if a user modifies a password (either upon initial creation or updating it) before it hashes it.
There are no problems with this on a post route, but if I use a patch route there are, and my middleware doesn't run.
Can someone help me understand in a more simple way (i.e. plain English) why this bypasses mongoose?:
const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true })
This works, though:
const user = await User.findById(req.params.id)
updates.forEach((update) => user[update] = req.body[update])
await user.save()