// 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
}
)
}
这是我的主要功能。我想将“ checkPassword”设置为“ checkPasswordService”的返回值。而且,这是我的“ 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
}
})
}
}
我看到了“ matttched1”的console.log。但是,在主要功能中,console.log(returnVal)是'undefined'。如何从checkPasswordService获取'true'或'false'的值?
答案 0 :(得分:4)
您在return
中没有matchPassword()
返回bycrypt
承诺
return bcrypt.compare(passwordInput, passwordDB).then((isMatch) => {..
答案 1 :(得分:1)
您的代码有问题。您既在“等待”承诺checkPassowrdService
,也在结尾使用.then()
。两者都有相同的目的。
我建议您在末尾删除.then()
并改用以下代码:
const returnVal = await checkPasswordService.matchPassword(password, auth.password)
returnVal = checkPassword