0

I made a code that receives the user's CPF then send's to the server, it checks if the CPF is valid, if it is, then print in the console the user's name, if it isn't, print "Not Found", this is the Code:

login.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>login</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-card>
    <ion-card-header clas="ion-text-center">
      <ion-card-title>Login</ion-card-title>
    </ion-card-header>
    <ion-card-content>
      <form>
        <ion-item>
          <ion-label position="floating" color="primary" for="cpf">Digite seu CPF</ion-label>
          <ion-input required id="cpf" name="cpf" type="text" [(ngModel)]="usuario.cpf"></ion-input>
        </ion-item>
        <div class="ion-padding-top">
          <ion-button shape="round" expand="block" (click)="login()">Login</ion-button>
        </div>
      </form>
    </ion-card-content>
  </ion-card>
</ion-content>

login.page.ts

import { AuthLoginService } from './../Services/auth-login.service';
import { Usuario } from '../classes/usuario';

export class LoginPage implements OnInit {

  private usuario: Usuario = new Usuario();

  constructor(private authService: AuthLoginService) { }

  login() {
    this.authService.validar(this.usuario) 
  }
}
usuario.ts

export class Usuario {

    cpf: string;
    senha: string;
    nome: string;
    numero_registros: number;
    id: string;
    id_na_academia: string;
    nome_academia: string;
    id_academia: string;

}

auth-login.service.ts

import { Usuario } from "../classes/usuario";

interface respostaAluno {
  ALUNO_ACADEMIA: any;
}

export class AuthLoginService {

  constructor(private http: HttpClient) { }

  validar(usuario: Usuario) {
      if(this.usuarioExiste(usuario)) {

        this.receberUsuario(usuario)

      } else {

        console.log('Usuário não encontrado')

      }
  }

  usuarioExiste(usuario): boolean {
    this.http
      .get<UsuarioDados>(environment.url + environment.token + "&Metodo=alunoCheckCPF&AlunoCPF=" + usuario.cpf)
      .subscribe(response => {
        usuario.numero_registros = response.ALUNO_ACADEMIA.Registros;
      })

      if (usuario.numero_registros > 0) {
      return true;

    } else {
      return false;
    } 
  }

receberUsuario(usuario) {
    this.http
      .get<UsuarioDados>(environment.url + environment.token + "&Metodo=alunoCheckCPF&AlunoCPF=" + usuario.cpf)
      .subscribe(response => {
        usuario.nome = response.ALUNO_ACADEMIA.AlunoDados[0].nome_aluno;
        usuario.id = response.ALUNO_ACADEMIA.AlunoDados[0].id_aluno_main;
        usuario.id_na_academia = response.ALUNO_ACADEMIA.AlunoDados[0].id_aluno_academia;
        usuario.senha = response.ALUNO_ACADEMIA.AlunoDados[0].aluno_senha;
        usuario.nome_academia = response.ALUNO_ACADEMIA.AlunoDados[0].academia_nome;
        usuario.id_academia = response.ALUNO_ACADEMIA.AlunoDados[0].id_academia;
      });
  }

Same logic as before but kind of different>

In my usuarioExiste() i make a request and grap the number of registers that the user has, if > 0 return true, if == 0 return false, but seems like the request don't make it in time, when the verification is executed the response did not arrived yet, so it always return false, how can i say to all my functions that makes requests to the server to only call the functions under them when the response of the requests has arrived?

Ban
  • 51
  • 6
  • 1
    Does this help answer your question? https://stackoverflow.com/questions/42364184/why-use-operator-in-template-binding – Reactgular Dec 24 '19 at 13:00
  • Unfortunately no, i'm not printing anything in the template, my problem is with a function in typescript, as the request takes a while to happen, instead of printing what it was suppose to print, print something like the user were not found, because the value did not yet get to the variable. – Ban Dec 24 '19 at 16:57

1 Answers1

0

I prefer to write it like this (as async method) :

  async validar(cpf) {

    let data = await this.receberAluno(cpf).toPromise();

    if(data.registros > 0) {
      console.log(data.nomeAluno)

    } else {
      console.log("Not Found")
    }
  }

receberAluno(cpf) {
   return this.http
      .get<respostaAluno>( environment.url + environment.token + "&Metodo=alunoCheckCPF&AlunoCPF=" + cpf)
  );
  }
  • Oh it worked! Thanks, unfortunetly it isn't the same code anymore :( But thanks a lot for the help!! – Ban Dec 25 '19 at 03:43