0
I have a list that is loaded from firebase. This list has 3 attributes, value, id, and date. Only I will need to capture only one of these attributes and assemble an array with them, to then perform a mathematical calculation, I need to extract the property (value). I searched on map, and subscribe, but could not get a satisfactory result, always returns me [Object Object] on 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);
}
I used as you passed, I just couldn’t see the information on the console. It didn’t bring me anything
– Diego Estacho
If you can include your code
buscarRecompensa()
, kindly– Patrick Lima
Here is my search codeRecompensates() It comes from a Provider : // This method will return a list of rewards searchRecompensates(rewardSalva: Boolean) { Return this.afs . Collection<Reward>(this.path, ref => { Return ref.Where('rewardSalva', '==', rewardSalva); }) . snapshotChanges() . map(actions => { Return actions.map(a => { const data = a.payload.doc.data() as Reward; const id = a.payload.doc.id; Return { id, ...data }; }) }); ; }
– Diego Estacho
My doubt was whether the return is a
observable
same, bySnapshotChanges()
is yes. Your view is updating normally?– Patrick Lima
This, my view updates normally. I display all rewards that are registered, with value and date.
– Diego Estacho
Got it. When you’re calling your method
teste()
? I believe then to be the asynchronous value still not ready when you sign up on it, ideal would be to call thisteste()
with a button that only calls after the view with the value– Patrick Lima
That’s exactly what I’m doing, when the view loads I put a button, I click on that button after the rewards are loaded, to then run this test function()
– Diego Estacho
I don’t understand why he wouldn’t show up on the subscribe so... it might be a silly test, but he could try it on his method:
this.recompensas.subscribe(recs => console.log(recs))
– Patrick Lima
He hasn’t returned anything either, I’m finding it strange too
– Diego Estacho
Is your method really being fired? Tested put a
console.log('etc')
in the same method without anysubscribe
?– Patrick Lima
Yes, he does call. I’ve done the following now: this.rewardss.map(Recs => this.valorTotal = Recs.map(rec => rec.data )) assigns the value to this.valorToral, but when printing it it appears as Undefined
– Diego Estacho
instead of rec.data is rec.valor, I ended up typing wrong
– Diego Estacho
How strange. I think I could only really help you with the full code, because for me I can’t imagine that the view can map these values but in one
subscribe
or even inmap
is not possible.– Patrick Lima
I appreciate all the help you’ve given to trying to help. I managed to solve the problem, I will post the code here later, if anyone needs or goes through the same problem.
– Diego Estacho
Opa, how great! Post yes, sure will be useful for those who already had, will have and even for knowledge.
– Patrick Lima