How to get data from an observable firebase?

0

I have a list that loads from the firebase. This list has 3 attributes, value, id, and date. But I'm going to need to capture just one of these attributes and mount an array with them, so I can do a math calculation, so I need to extract the property (value) from there. I researched map, and subscribe, but I could not get a satisfactory result, it always returns me [object object] in the console. Could someone help me?

  recompensas : Observable<Recompensa[]>;//meu observable
  public recompensa = {} as Recompensa;//meu model de dados

  //exibindo a minha lista na view
  ionViewDidLoad() {
     this.recompensas = this.recompensaProvider.buscarRecompensa(true);
  }  


  //minha tentativa de realizar o map e subscribe, sempre obtenho [object 
  //Object] no console
  teste() {
      const test = this.recompensas.subscribe(recs => recs.map(rec => 
      rec.valor ));
      console.log('valor de teste : '+test);
  }

  teste() {
      const test = this.recompensas.map(recs => recs.map(rec => 
      rec.valor ));
      console.log('valor de teste : '+test);
  }

    
asked by anonymous 05.11.2018 / 03:22

1 answer

1

In your teste() method you do

teste() {
  const test = this.recompensas.subscribe(recs => recs.map(rec => 
  rec.valor ));
  console.log('valor de teste : '+test);
}

In this case, the const test is a type Subscription because it is taking the value of your subscribe , you are subscribing correctly but to get the value you must search it inside the subscribe, something like this might work:

teste() {
  this.recompensas.subscribe(recs => {
    console.log('Seu array: ' + recs);
    recs.map(rec => console.log('Array mapeado por valor: ' + rec.valor))
  });
}
    
05.11.2018 / 12:41