-2

I have this function on the server side, forced to return true for the moment:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        [HttpGet("cPasaAJefeVentas")]
        public async Task<IActionResult> CPasaAJefeVentas(string idPais, string idCompania, string idSalesorg, string idDivision, string idUser, decimal porcentaje)
        {
          
            return Ok(true);

        }

This is how you call it on the client side inside a service:

cPasaAJefeVentas(idPais: string , idCompania:string, idSalesOrg:string, idDivision:string, idUser:string, porcentaje:number) {
        return this.http.get(this.altapedidosserveruri + 'cPasaAJefeVentas?=' + idPais + '&idCompania=' + idCompania + '&idSalesOrg=' + idSalesOrg + '&idDivision=' + idDivision + '&idUser=' + idUser + '&porcentaje=' + porcentaje);
    }

And here is where i'm having issues right now:

 cPasaJefeVentas(): boolean {
    let descuentoDiscrecional: number = 5;
    let res: boolean = null;
    this.altaPedidosService
      .cPasaAJefeVentas(this.pedidoObjeto.idPais, this.pedidoObjeto.idCompania,
        this.pedidoObjeto.idSalesOrg, this.pedidoObjeto.idDivision, this.loggedService.logged.UserName, descuentoDiscrecional)
      .subscribe((result: boolean) => (
        console.log(result),
        res = result
      ))
    return res;
  }

The console.log(result) prints TRUE, which is correct, but then i want to asignt that returned value to the "res" variable, and its not working, because it's still null when it exits the .susbscribe.

I want to know what am i doing wrong here? Why isnt the value of the "res" variable updating like it should?

FranciscoFJM
  • 119
  • 1
  • 10

1 Answers1

0

Your service is returning an Observable response. Observables are asynchronous and you can't guarantee when the response will receive. So, modify your method to return Observable response as well. Your code would get modified like this

 cPasaJefeVentas(): Observable<boolean> {
    let descuentoDiscrecional: number = 5;
    let res: boolean = null;
    let response$ = this.altaPedidosService
      .cPasaAJefeVentas(this.pedidoObjeto.idPais, this.pedidoObjeto.idCompania,
        this.pedidoObjeto.idSalesOrg, this.pedidoObjeto.idDivision, this.loggedService.logged.UserName, descuentoDiscrecional)
      .subscribe((result: boolean) => (
        console.log(result),
        res = result
      ));
      
      return response$;
  }

And in the template you may apply async pipe to show the observable value, for example

<p>{{cPasaJefeVentas() | async}}</p>
Obaid
  • 2,563
  • 17
  • 15