I'm trying to implement a register system, and I have to validate the mail and check that it's not already being used by another account in the database. The problem is that the validator function for email must be asynchronous as the query is asynchronous, but the verifyUser function that among other things verifies the email must be a normal function.
To the code!
async function _verifyEmail(User){
const { email } = User;
var isAvailable = await UserModel.countDocuments({email: email}).then(res => isAvailable = res > 0 ? false : true);
var isValid = EmailValidator.validate(email)
console.log(`isValid: ${isValid}`)
console.log(`isAvailable: ${isAvailable}`);
if(!!isValid || !isAvailable)
return 400;
else
return 202;
}
function _verifyUser(User){
var emailStatus;
_verifyEmail(User).then(res => {
emailStatus = res;
console.log(`res: ${res}`);
});
const passwordStatus = _verifyPassword(User);
const nameStatus = _verifyName(User);
const surnameStatus = _verifySurname(User);
console.log(`email status: ${emailStatus}`);
console.log(`password status: ${passwordStatus}`);
console.log(`name status: ${nameStatus}`);
console.log(`surname status: ${surnameStatus}`);
if(emailStatus === 400 || passwordStatus === 400 || nameStatus === 400 || surnameStatus === 400)
return 400;
else
return 202;
}
The problem
When I run this code while every other parameter in _verifyUser is fine, emailStatus is unavailable since the _verifyEmail function returns after the if computation.
How can I solve this?
Top comments (6)
I think you can just async await the callback.
I think you are the most intelligent person in the world.
I'll try this right away! Thanks!
Edit: unfortunately same result, it's still not working for some reason...
I think I have found the problem, you're console logging res instead of emailStatus.
I'm console logging both, but that's not the problem. The problem is that when _verifyEmail ends the result is a promise instead of an actual value.
Edit:
When I execute this code:
I get this as a result:
but _verifyUser should be a 400 (look at res) since there is already a user with the mail I've sent.
Ok, the problem is you cannot pass a value to emailStatus coming from the .then method:
I have encountered this problem before but I forgot how I got to the bottom of it.
This post should help.
I think there are two ways to solve this problem,
The first one requires you to add an async in _verifyUser:
The second one is a little bit experimental because i don't know if it will work:
Edit: Come to think of it this will not work, it will only return a promise I think.
Because that would require enormous updates in my codebase as this function is contained into a _saveUser function that is contained into a _createUser function that returns to the express router the status code to return to the client through HTTP