Call a function inside async function at a js class
login
doesn't refer to userPassword
method but to the function of the same name which doesn't exist.
Promises are supposed to be be chained and they aren't. userPassword
is expected to return a promise but it uses obsolete Mongoose callback API.
That UnhandledPromiseRejectionWarning
is shown means that errors weren't correctly handled in login
while they should. As explained in this answer, Express don't support promises so errors should be handled by a developer.
It should be:
async login(req, res) { try { const { email, password } = req.body var hashPassword = await this.userPassword(email) return res.status(200).json({ 'msg': 'Log in OK!' }) } catch (err) { // handle error } } async userPassword(email) { const { password } = await User.findOne({ email: email }); return password; }
this error is coming because you are not handling error for the promise. Always use async/await inside try/catch block.
try{ async login(req, res) { const { email, password } = req.body console.log('step 1') var hashPassword = await userPassword(email) console.log(hashPassword) console.log('step 2') return res.status(200).json({ 'msg': 'Log in OK!' }) }}catch(e){ console.log(e)}