-1

I'm trying to get a value from a service. I need to take this value and send to other service. The code is like this:

datameterId: string;

  ngOnInit() {
    this.graphCtrl.getMeter(this.id).subscribe(
            response => {
              this.ultimoValor = response["body"]["ultimoDatoConocido"];
              this.ultimoValorCierre = response["body"]["ultimoDatoCierreMes"];
              this.consumoMediaDiaria = response["body"]["consumoMedioDiario"];
              this.consumoMediaMensual = response["body"]["consumoMedioMensual"];
              this.consumoUltimoMes = response["body"]["consumoUltimoMes"];
              this.alertas = response["body"]["alarmasActivas"];
              this.contador = response["body"]["resumenDatameter"]["contador"];
              this.datameterId = response["body"]["resumenDatameter"]["id"];
            },
            error => {
              console.log(error);
            }
          );
        this.config();
         }

config() {
     console.log(this.datameterId); //NULL
}

The problem: When I call another function the value lost, so i can´t use it. What´s is wrong in the code?

UPDATE

If i put this.config(); in the response returns

Cannot read property 'every' of undefined
    at DaterangepickerComponent.push..

UPDATE 2

If i delete

 this.ranges = {
      Hoy: [moment(), moment()],
      Ayer: [moment().subtract(1, "days"), moment().subtract(1, "days")],
      "Últimos 7 días": [moment().subtract(6, "days"), moment()],
      "Últimos 30 días": [moment().subtract(29, "days"), moment()],
      "Este mes": [moment().startOf("month"), moment().endOf("month")],
      "Mes pasado": [
        moment()
          .subtract(1, "month")
          .startOf("month"),
        moment()
          .subtract(1, "month")
          .endOf("month")
      ]
    };

Doesn´t show error, but doesn´t works. Datarange need ranges to work.

El Hombre Sin Nombre
  • 2,906
  • 18
  • 49
  • 92

2 Answers2

1

This is wrong because you call function and dont wait to value. You need call function inside a subscribe or use async/await

 this.graphCtrl.getMeter(this.id).subscribe(res =>{
\\Do your stuff
this.config();

})
silvelo
  • 377
  • 1
  • 7
0

Call it after subscribed

 ngOnInit() {
    this.graphCtrl.getMeter(this.id).subscribe(
            response => {
              this.ultimoValor = response["body"]["ultimoDatoConocido"];
              this.ultimoValorCierre = response["body"]["ultimoDatoCierreMes"];
              this.consumoMediaDiaria = response["body"]["consumoMedioDiario"];
              this.consumoMediaMensual = response["body"]["consumoMedioMensual"];
              this.consumoUltimoMes = response["body"]["consumoUltimoMes"];
              this.alertas = response["body"]["alarmasActivas"];
              this.contador = response["body"]["resumenDatameter"]["contador"];
              this.datameterId = response["body"]["resumenDatameter"]["id"];


            this.config();<***************

            },
            error => {
              console.log(error);
            }
          );

         }
Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39