0

I have this ngOnInit. the first subscribe works well and brings the value. The second one calls the backend, get the response (a non-null object) but it´s not setting this.taxas with the reponse value (yes, the type is correct and I can see the return on Postman). I guess it might be a problem because I´m calling multiples subscribes on ngOnInit but I don´t have any ideia how to fix it. Does someone have any idea?

  ngOnInit() {

    this.sharedService.getMonthValueObservable().subscribe(x => {
      this.monthValue = x;
    });

    this.simuladorGvService.obterTaxasRecebaRapido(this.monthValue).subscribe(
      response => {
        this.taxas = response;
    });

    console.log(this.monthValue);
    console.log(this.taxas);

  }
bryan60
  • 28,215
  • 4
  • 48
  • 65

1 Answers1

1

this is a switchMap use case:

import { switchMap } from 'rxjs/operators';

...

this.sharedService.getMonthValueObservable().pipe(
  switchMap(x => {
    this.monthValue = x;
    return this.simuladorGvService.obterTaxasRecebaRapido(x)
  })
).subscribe(response => {
  this.taxas = response;
  // log in subscribe, not set till it finishes executing.
  console.log(this.monthValue);
  console.log(this.taxas);
});
bryan60
  • 28,215
  • 4
  • 48
  • 65