These are chat archives for Automattic/mongoose

18th
Jan 2018
Jin Shun Lee
@jinshunlee
Jan 18 2018 04:08

In document middleware functions, this refers to the document.
init
validate
save
remove

In query middleware functions, this refers to the query.
count
find
findOne
findOneAndRemove
findOneAndUpdate
update

does anyone know how to get the document in the "update" pre hook middleware? using this gets the query only, but i want the document like in the "save" middleware.

Adie Williams
@adieCodes
Jan 18 2018 12:16
Hi all. I've got a really basic question but I can't find an answer to it anywhere. I'm looking to up my error handling game but it's not clear to me how mongoose chooses which error to throw, I've been trying to find some clarify on what a CastError is but I can't find anything definitive. Please can someone point me to a useful resource, thanks
Kev
@lineus
Jan 18 2018 15:28
@adieCodes have you tried looking at the source in node_modules/mongoose/lib/error/cast.js?
Kev
@lineus
Jan 18 2018 15:43
you can see the context of where it gets thrown with
find node_modules/mongoose/lib  -type f | xargs grep 'throw new CastError'
Adie Williams
@adieCodes
Jan 18 2018 20:26
That's great @lineus didn't know that existed. Will check it out tomorrow. Thank you