Greetings, this is my debut post here so my apologies if I am not following the correct procedures.
As a novice in Angular with no experience in synchronism, I kindly request that any explanation be kept as simple as possible.
I am currently working with a function in my service:
validateDDBB(userName, teamName, email: string) {
let requestUrl: string = '/api/tenniship/validator/user?userName=' + userName + '&teamName=' + teamName + '&email=' + email;
const config = { headers: new HttpHeaders().set('Content-Type', 'application/json') };
return this.http.get<Array<boolean>>(requestUrl, config);
}
This function is intended for a Sign Up form. It serves to check if an email, username, and teamName are already registered in the database. The function returns a boolean array with a size of 3, indicating their existence or absence.
The issue I am facing is that the code is not waiting for the response, resulting in the use of incorrect data.
Here is my component function:
databaseValidator(){
this.loginService.validateDDBB(this.username, this.teamName, this.email).subscribe(
res => {
this.usedUsername = res[0].valueOf();
this.usedTeamName = res[1].valueOf();
this.usedEmail = res[2].valueOf();
console.log("Data base info pulled: " + res);
return true;
},
error => {
console.error("Something went wrong: undefined: " + error);
return false;
}
);
}
How can I adjust my code so that when I call this function, it waits for the response and avoids using incorrect data?
onSubmit(){
this.databaseValidator();
//usedUsername, usedTeamName, and usedMail are boolean variables.
console.log("Username: " + this.usedUsername);
console.log("Team: " + this.usedTeamName);
console.log("email: " + this.usedEmail);
}
Thank you very much!