1
// Password match var
var checkPassword = null;

// Find user by email
const auth = await Auth.findOne({ userEmail }, { userLoginInfo: 0 });

// If user does not exist
if (auth === null) {
  return res
    .status(400)
    .json({ authFailedMessage: 'Email or password is incorrect' });
} else if (auth !== null) {
  // Check password when user exists
  const returnVal = await checkPasswordService.matchPassword(
    password,
    auth.password 
  ).then((returnVal) => {
    console.log(returnVal) ----> undefined
    returnVal = checkPassword
  }
  )
}

This is my main function. I want to set 'checkPassword' to the return value from 'checkPasswordService'. And, this is my 'checkPassowrdService'.

class checkPasswordService { 
    static async matchPassword(passwordInput, passwordDB) {
        console.log('passint', passwordInput)
        console.log('passDB', passwordDB)
       await bcrypt.compare(passwordInput, passwordDB).then((isMatch) => {
           if(isMatch) {
               console.log('matttched!') -------->returning 'mattched!'
               return true
           } else {
               return false
           }
       })
    }
}

I see the console.log of 'matttched1'. However, in the main function, console.log(returnVal) is 'undefined'. How can I get the value of 'true' or 'false' from checkPasswordService?

1
  • You are mixing the async/await with callbacks, you should use only one of them. Commented Aug 22, 2020 at 19:51

2 Answers 2

4

You have no return in matchPassword()

Return the bycrypt promise

return bcrypt.compare(passwordInput, passwordDB).then((isMatch) => {..
Sign up to request clarification or add additional context in comments.

Comments

1

There's a problem with your code. You are both 'awaiting' for the promise checkPassowrdService as well as using a .then() at the end. Both serve the same purpose.

I suggest you remove the .then() at the end and use the following piece of code instead:

const returnVal = await checkPasswordService.matchPassword(password, auth.password)
    returnVal = checkPassword

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.