It's my first time posting here so I first apologize if I'm doing something wrong.
I'm so new in Angular and I've never used synchronism, so please make it as simple as possible.
I have this 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);
}
I want it for a Sign Up form. I want to know if an email, username and a teamName are already registered in my database. This function sends me a boolean array of size = 3 which indicates if they already are or not.
The problem is that the code running does not wait for the response, so the results obtained use garbage data.
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 modify my code so, for example, when I do this my code will wait for the response so I do not use garbage data?
onSubmit(){
this.databaseValidator();
//usedUsername, usedTeamName and usedMail are primitive boolean variables.
console.log("Username: " + this.usedUsername);
console.log("Team: " + this.usedTeamName);
console.log("email: " + this.usedEmail);
}
Thanks lots!