mongoose findOneAndUpdate returns null for both err and doc (using Jest)
I thank you for explaining the issue that well.It is mentioned in the official documentation of MongoDB here that findOneAndUpdate
updates the first document in the collection that matches the request and return the original document.
I supposed that both err
and doc
are null because of the document that you want to update doesn't exist in your collection. It is not mentioned in the documentation what is the behavior of findOneAndUpdate
if there is no document that matches the query. So, I tried to go deeper into it.
I saw in the documentation (you can learn more about it here) that if the parameter upsert
isn't mentioned, it will take false
as a default value. And I saw here in the P.S. part that if upsert
is false
and the document doesn't exist, than doc
will be null
.
EDIT: not correct - see other answer. I got it figured out. I was returning the variable asynchronously before it had a value, so it wasn't a findOneAndUpdate
issue after all. :/