-1
Check it out, I have a code where the person type her number, check if the user exists or not, if yes, search his data, check if he has password or not, if you have goes to a page if you don’t have go to another.
What’s the matter?
I have that code:
async validacao(usuario) {
if (this.usuarioExiste(usuario)) {
this.receberUsuario(usuario);
this.authLoginService.senhaExiste(usuario);
} else {
console.log('Não encontrado');
}
}
async usuarioExiste(usuario) {
await this.http
.get<UsuarioDados>(environment.url + 'Metodo=alunoCheckCPF&AlunoCPF=' + usuario.cpf)
.toPromise()
.then(response => {
usuario.numero_registros = response.ALUNO_ACADEMIA.Registros;
});
if (usuario.numero_registros === 1) {
return true;
} else {
return false;
}
}
async receberUsuario(usuario) {
await this.http
.get<UsuarioDados>(environment.url + 'Metodo=alunoCheckCPF&AlunoCPF=' + usuario.cpf)
.toPromise()
.then(res1 => {
usuario.nome = res1.ALUNO_ACADEMIA.AlunoDados[0].nome_aluno;
usuario.id = res1.ALUNO_ACADEMIA.AlunoDados[0].id_aluno_main;
usuario.idNaAcademia = res1.ALUNO_ACADEMIA.AlunoDados[0].id_aluno_academia;
usuario.senha = res1.ALUNO_ACADEMIA.AlunoDados[0].aluno_senha;
usuario.nomeAcademia = res1.ALUNO_ACADEMIA.AlunoDados[0].academia_nome;
usuario.idAcademia = res1.ALUNO_ACADEMIA.AlunoDados[0].id_academia;
});
await this.http
.get<UsuarioContatos>(
environment.url + 'Metodo=alunoLoginContatos&AlunoIDMain=' + usuario.id + '&CttoValidacao=true'
)
.toPromise()
.then(res2 => {
usuario.celular = res2.ALUNO_CONTATOS.Contatos[0].Contato;
usuario.email = res2.ALUNO_CONTATOS.Contatos[1].Contato;
});
this.armazenarUsuario(usuario);
}
The idea is that if the number of user records is 1, ie the user exists, returns true, if it is 0, it does not exist, then returns false.
The function validacao()
checks the function return usuarioExiste()
was supposed to return true, perform the two other functions receberUsuario()
and senhaExiste()
, if returned false, give a console.log('Não encontrado')
, So I took two numbers, one valid and one not.
Valid runs perfectly, receives the user, checks for password, redirects to the page, all a marvel, already invalid not.
Invalid even the function usuarioExiste()
be returning false, the function validacao()
is allowing it to perform the receberUsuario()
and I don’t really know why, someone could help me?
The problem is that the method
usuarioExiste
isasync
, then there’s no way he can return aboolean
, he will return aPromise<boolean>
. You need to treat this feedback usingawait
for example take the value of this.– Andre