0
I’m trying to do something simple. It happens that I need to obtain the quantity of stock of that item, at that given moment to then make the return and the feed of the stock correctly. I’ve built the entire algorithm, but I’m having a hard time sending an update to my firebase product node.
1º Whenever I try to show the value of the object outside of subscribe, it returns Undefined.
2º I can’t work with any of his property outside of the subscribe, so I can’t make the sum of the return and stock quantities.
3º It only works if I update inside the subscribe, but it returns me an infinite loop, enough to lock my browser, now I do not know how to proceed in this case.
My code is super simple:
devolverItem(item: any){
let pathItens = `/itens/`;
this.itensFirebase = this.db.list(pathItens, {
query: {
orderByChild: 'key',
equalTo: item.key
}
});
this.itensFirebase.subscribe( data => {
this.itemEstoqueSemGrade = data;
})
//Aqui ele me retorna sempre o undefined
console.log(this.itemEstoqueSemGrade);
}
This is an example of how "it works", but with the loop:
devolverItem(item: any){
let pathItens = `/itens/`;
this.itensFirebase = this.db.list(pathItens, {
query: {
orderByChild: 'key',
equalTo: item.key
}
});
this.itensFirebase.subscribe( data => {
this.itemEstoqueSemGrade = data;
this.quantidadeItemAtualizarEstoqueTotal =
this.itemEstoqueSemGrade[0].quantidade + item.quantidadeDevolver;
this.db.object(`/itens/${item.key}/`).update({
quantidade: this.quantidadeItemAtualizarEstoqueTotal
})
})
}
This second option in my view does not make any sense, I understand that I would have to do it out of there, but if I try to work with the data of my itemEstoqueSemGrade out of there, it always returns me Undefined, so I can not perform any operation.
Any help will be welcome..
Diego, I improved the code by putting an example with your code, only after I had published I saw that you were using Angularfire and improved the response.
– Rodolfo Patane