Model copular only with model data at angular

Asked

Viewed 23 times

0

I am currently making a request get in the backend but is returning data that I will not use, I would like to copulate my model only with the data that was created.

My model:

export class Cliente {

  public nome: string;
  public cnpj_rg: string;
  public cpf_inscricao: string;
  public email: string;
  public telefone: string;
  public sexo: string;
  public data_nascimento: string;
  public oferta_email: boolean;
  public informacoes_sms: boolean;

}

My http request:

  recuperaUsuario() : void {
    this.loadingService.alteraEstadoLoading(true)
    this.clienteService.recuperarCliente()
    .pipe(take(1))
    .subscribe((res ) => {
      this.cliente = res.body;
      this.loadingService.alteraEstadoLoading(false)
    },
    (err: HttpErrorResponse) => {

    })
  }

My service:

recuperarCliente() : Observable<any> {
  return this.http.get<Cliente>(AppSettings.API_ENDPOINT + 'clientes/dados-cadastrais', {observe: 'response'})
}

My problem is that my backend is returning more keys than necessary and my model ends up being copied by them causing me some problems when sending this data to the backend.

Is there any way I can copulate only the keys in my model?

  • 1

    vc have to use the map operator and map the object manually.

  • You can provide me with an example of how to do this?

1 answer

1

Follow an example:

recuperarCliente() : Observable<ClienteState> {//Gosto de usar state no final de entidades que contem o estado da aplicação
  return this.http.get<Cliente>(AppSettings.API_ENDPOINT + 'clientes/dados-cadastrais').pipe(
           map(response => {
              return {
                name: response.name,
               ...etc
             }
           })
    ) // sem o state para entidades de payload do serviço do backend
}

vc tbm can use delete to remove only a few keys for example:

delete response['propriedade'];
  • I get it. There’s no way to use the model right? I wish I didn’t have to type the name of all the keys because I already typed them in the model...

Browser other questions tagged

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