So I am trying to create a basic authentication app without using any modules or auth strategies. Just by pushing user credentials to a MongoDb database and then by comparing the entered credentials every time a user tries to login with the existing ones.
For some reason i am not able to filter out the mongodb document with the same username entered by the user. When i try to log it to the console i am getting a query , i am expecting an object so that both the req.body.password and user.password can be compared.
2
Answers
The User.findOne function returns a Query object, which represents the pending query operation. To retrieve the actual user document, you need to execute the query using .exec().
You can modify your function in this way:
Let me know, If still the issue persist.
Mongoose’s
findOne
returns aQuery
, which you need to execute to get a Promise containing the actual object. If you change the relevant lines to the following, you should be fine: