Requisition Delay

别说谁变了你拦得住时间么 提交于 2020-01-06 05:36:07

问题


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?


回答1:


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)
  );
  }


来源:https://stackoverflow.com/questions/59469145/requisition-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!