Skip to content
Advertisement

MongoDB returning an onject that doesnt exist

I was working on a project with MongoDB and passport, when i ran into this error, event though p1 isn’t used, it still reruns an object im guessing, because it just says that the field p1 is taken, when it isn’t. The same is happening with p2. Does anyone know why ?

JavaScript

Advertisement

Answer

Calling User.find returns a Promise which you are not awaiting. So when you are checking for existence of p1 and p2, it returns a truthy value as both values are Promise objects.

To fix the issue use await in front of both User.find like this

JavaScript

After that both values will be array as you are using find method so just check for length property or better use findOne instead of find method.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement