Home > Mobile >  Why is the value not assigned to a variable inside forEach in Angular?
Why is the value not assigned to a variable inside forEach in Angular?

Time:05-15

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?

CodePudding user response:

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;
    }

CodePudding user response:

I tried using the method you wrote, but I couldn't. My service method are these 2 that I'm trying.

obterParametrosgeraisMap(): Observable<any> {
    return this.http.get(this.URL   'parametros/listar-parametros-gerais/')
   .pipe(map(response => response));
}

obterParametrosEspecificosMap(): Observable<any> {
    return this.http.get(this.URL   'parametros/listar-parametros-especificos')
    .pipe(map(response => response));
}
    
  • Related