'Why is the value not assigned to a variable inside forEach in Angular?

how can I have the return of the variable "this.qdDias" assigned a value to it. I don't know why when I use subscribe I can't get the value at the end of the method. In this "console.log(this.qdDias) " I see the value, but it lowers in the return, the variable becomes undefined again.

> obterParametroPrazoMap(nomePrazo: string): number {
>         console.log("Parametro vindo: ", nomePrazo)
>         this.service.obterParametrosgeraisMap().subscribe((data) => {
>             this.listParametrosGerais = data.data;
>             this.listParametrosGerais.forEach((x) =>  (this.listParametrosGerais, x));
>             for (var i = 0; i < this.listParametrosGerais.length; i++) {
>                 console.log("comparado: ", this.listParametrosGerais[i].chavePrimaria.tipoParametro.nomeTipoParametro)
>                 if (this.listParametrosGerais[i].chavePrimaria.tipoParametro.nomeTipoParametro
> === nomePrazo) 
>                 {   
>                     this.qdDias = this.listParametrosGerais[i].quantidadeDiasPrazo;
>                     console.log(this.qdDias)                    
>                     break
>                 }
>             }
>         }                   
>         );
>        return this.qdDias;
>     }

How to solve this?



Solution 1:[1]

I resolved this that way:

Promise.resolve(2);
        Promise.all([
           this.service.obterParametrosgeraisMap()
                .then((lista: any) => {
                    this.listParametrosGerais = lista.data;
                })
            ,
 this.service.obterParametrosEspecificosMap()
                .then((lista: any) => {
                    this.listParametrosEspecifico = lista.data;
                })
        ]).then(() => {    
            this.iniciarVariaveisDicionario();
 });

Solution 2:[2]

return this.qdDias is out of subscribe scope. But even if the return would be in subscribe() it would still not work. You are subscribing to an observable this.service.obterParametrosgeraisMap().subscribe() which by nature is asynchronous. But you are trying to treat this function as a synchronous.

Is this.service.obterParametrosgeraisMap() an observable that you can subscribe to? Then it should be this.service.obterParametrosgeraisMap.

What this does? this.listParametrosGerais.forEach((x) => (this.listParametrosGerais, x));

Why use var? Should be const or let?

This could somewhat work but you have to decide if you want to return an observable out of which you take the value. Or you convert it and resolve a promise to keep async pattern https://stackoverflow.com/a/42185519/15439733 or something else.

    function obterParametroPrazoMap(nomePrazo: string): number {
        console.log('Parametro vindo: ', nomePrazo);
        let map;
        this.service.obterParametrosgeraisMap.pipe(take(1)).subscribe(data => map = data);

        this.listParametrosGerais = map?.data;
        this.listParametrosGerais.forEach((x) => (this.listParametrosGerais, x));
        for (var i = 0; i < this.listParametrosGerais.length; i++) {
            console.log('comparado: ', this.listParametrosGerais[i].chavePrimaria.tipoParametro.nomeTipoParametro);
            if (this.listParametrosGerais[i].chavePrimaria.tipoParametro.nomeTipoParametro === nomePrazo) {
                this.qdDias = this.listParametrosGerais[i].quantidadeDiasPrazo;
                console.log(this.qdDias);
                break;
            }
        }

        return this.qdDias;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BDehylz Lima
Solution 2 Joosep Parts