0

In Angular 9, I'm getting data from subscribe() inner forEach. I can see that my variable is loaded with data, but Angular is not rendering. I suspect that Angular is rendering before my variable is loaded.

How can I resolve it?

Called function

this.servicePromotion.list(text)
    .subscribe((res: any) => {
        this.gridList = [];

        if (res) {
          console.log('GRID LIST', this.gridList) // -> here I can show "cupom" property filled.
          this.gridList = this.mapperCalendatioStatus(res.list) // -> but when I try redering on screen, it's not shown
        }
    })

Function that loop "promocoes" and search p.id in a subscribe.

mapperCalendatioStatus(promocoes) {
    promocoes.forEach(p => {
          const promocoesStatus = [];
          let campaignExists = '';

          const consultaPromocao = new ConsultaPromocaoModel();
          consultaPromocao.id = p.id
          consultaPromocao.codigoIdentificacao = p.codigoIdentificacao;
          consultaPromocao.nome = p.nome;

          this.serviceCouponGenerate.getByIdPromocao(p.id).subscribe((response: any) => {
          if (response.length) {
            campaignExists = 'YES'
          } else {
            campaignExists = 'NO'
          }
          consultaPromocao.cupom = campaignExists;

          promocoesStatus.push(consultaPromocao);

    })

    return promocoesStatus;
}

Cupom column to be need filled

enter image description here

My variable filled (cupom: "SIM" for example)

enter image description here

3
  • There are multiple issues with your code: most of them are the lack of understanding between synchronous code and asynchronous code. Commented Sep 1, 2021 at 16:13
  • I would suggest you to read on, concatMap, pipe and specially forkjoin of rsjx since you should not be sending one coupon id at a time. Commented Sep 1, 2021 at 16:19
  • I'm read about it, still didn't quite understand as well, but if you show me an example from my thread probably would help me many more. :) Commented Sep 1, 2021 at 23:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.