Answer the question
In order to leave comments, you need to log in
How to wait for an asynchronous function to complete?
Hello there is a code:
var isLoginIn = false;
ajcom.register('login', (hCtx, email, pass) => {
User.findOne({
where: {
email: email
}
}).then(function(foundUser) {
if (foundUser && foundUser.password === pass) {
console.log("Авторизация прошла успешно!");
isLoginIn = tru;
}
else {
console.log("Неверные данные, проверьте ваш email и пароль!");
}
});
return {authorized: isLoginIn};
});
return {authorized: isLoginIn};
return the data after the request to the database, if the authorization is successful, then isLoginIn = true;
the fact is that the return {authorized: isLoginIn};
request does not wait, how are these situations solved?
Answer the question
In order to leave comments, you need to log in
ajcom.register('login', async (hCtx, email, pass) => {
const foundUser = await User.findOne({ where: { email } });
if (foundUser && foundUser.password === pass) {
console.log("Авторизация прошла успешно!");
isLoginIn = true;
} else {
console.log("Неверные данные, проверьте ваш email и пароль!");
}
return { authorized: isLoginIn };
});
If you do not go into too much detail, then you can do this:
User.findOne({
where: {
email: email
}
}).then(function(foundUser) {
if (foundUser && foundUser.password === pass) {
console.log("Авторизация прошла успешно!");
isLoginIn = tru;
}
else {
console.log("Неверные данные, проверьте ваш email и пароль!");
}
}).then(function() {
return {authorized: isLoginIn};
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question