Angular 8 - Picking up attributes on the return of the API service. httpClient

Asked

Viewed 304 times

0

I need help handling the attributes being returned from my API. I want to intercept them in my service class, but I’m not getting through using Subscribe.

export class Lancamentoservice {

  lancamentoUrl = 'http://localhost:8080/lancamentos';

  constructor(private http: HttpClient) { }

  pesquisar(filtro: any): Observable<Lancamento> {
    let params = new HttpParams();

    params = params.set('page', filtro.pagina);
    params = params.set('size', filtro.itensPorPagina);

    if (filtro.descricao) {
      params = params.set('descricao', filtro.descricao);
    }

    if (filtro.dataVencimentoInicio) {
      params = params.set('dataVencimentoDe', moment(filtro.dataVencimentoInicio).format('YYYY-MM-DD'));
    }

    if (filtro.dataVencimentoFim) {
      params = params.set('dataVencimentoAte', moment(filtro.dataVencimentoFim).format('YYYY-MM-DD'));
    }

    return this.http.get<Lancamento>(`${this.lancamentoUrl}?resumo`, { params } );

  }

I demarched the attributes I want to manipulate.

https://ibb.co/vqsdXGL

  • I had to edit my answer rs, you changed your question 3x hahah

  • Sorry, it’s the first time I’m looking for help in the stack.

  • No problem, see if my answer helps you, whenever you need can seek the community, if not comment there in the answer.

  • Thanks for your attention, I’ll take a look at the documentation of Rxjs that sent me the link and in case I don’t get I return here.

  • I gave an example using pipe with map in the service, I think it solves your problem

1 answer

1

Okay, you want to take the feedback from this Observable get, you can use a callback and listen to this method using a subscribe, it would be good if you took a look at the documentation and understand how Observable rxjs works, and see how it works in Angular.

export class ExampleComponent implements OnInit {
 constructor(
 private lancamentoService: LancamentoService ) { }

 ngOnInit() {
  // se você precisar que chame no ciclo de vida, adicionar aqui
 }

 pesquisarLancamentos() {
  this.lancamentoService.pesquisar().subscribe(res => {
    // res retorno do pesquisar
  });

 }
}

and to get only the same attributes of the image, you can use a pipe and map, and in the call back of the Component you will still use the subscribe to get the return.

return this.http.get<Lancamento>(`${this.lancamentoUrl}?resumo`, { params } ).pipe(map(res => // mapear seus atributos aqui
));

https://angular.io/guide/observables https://rxjs-dev.firebaseapp.com/guide/observable

Browser other questions tagged

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