Problem with Function Return, even returning false runs

Asked

Viewed 46 times

-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?

  • 1

    The problem is that the method usuarioExiste is async, then there’s no way he can return a boolean, he will return a Promise<boolean>. You need to treat this feedback using await for example take the value of this.

1 answer

1


The function usuarioExiste is asincrona, ie, your return is not a boolean, but rather a Promise<boolean>, then you need to wait for the promise to be long so make the check, you can do this with the then, but the simplest way is with async and await

In your call the function within the condition changes by:

if (await this.usuarioExiste(usuario)) {
    // ...
  • It worked perfect. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.