本文介绍了申请延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码,接收用户的CPF,然后将其发送到服务器,它检查CPF是否有效,如果有效,则在控制台中打印用户名,如果无效,则打印找不到" ,这是代码:

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>

在我的usuarioExiste()中,我发出一个请求并掌握用户拥有的寄存器数,if > 0返回true,if == 0返回false,但是似乎请求没有及时发出,执行验证后,响应尚未到达,因此它始终返回false,我该如何对所有向服务器发出请求的函数说,在请求的响应到达时才调用其下的函数?

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?

推荐答案

我更喜欢这样写(作为异步方法):

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

这篇关于申请延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:02