[英]Angular & Firebase Realtime-Database. How to get child knowing its id property but without knowing its parent node
My Database is like this我的数据库是这样的
I can get to cart part on my own and I can even delete the product from cart.. if I type in the auto generated id manually.. Honestly, I have been trying to do this for 2 days now.我可以自己进入购物车部分,甚至可以从购物车中删除产品。如果我手动输入自动生成的 ID。老实说,我已经尝试这样做 2 天了。 I researched every stackoverflow question related to firebase database but I still couldn't figure it out.我研究了与 firebase 数据库相关的每个 stackoverflow 问题,但我仍然无法弄清楚。
I get to cart like this: firebase.database().ref('/' + account[0] + '_user' + '/cart')
我像这样进入购物车: firebase.database().ref('/' + account[0] + '_user' + '/cart')
But I can't go further.. assume that I am also providing product that needs to get deleted and you can access its id through item.id
但是我不能进一步 go ..假设我还提供需要删除的产品,您可以通过item.id
访问其 ID
Right now if you click on delete button the whole cart gets deleted.现在,如果您单击删除按钮,整个购物车将被删除。
If you know the product/id
property of the node to delete, you can use a query to order/filter the nodes and find the key of the one(s) you want to delete:如果您知道要删除的节点的product/id
属性,则可以使用查询对节点进行排序/过滤并找到要删除的节点的键:
const cartRef = firebase.database().ref('/' + account[0] + '_user' + '/cart');
const itemQuery = cartRef.orderByChild("product/id").equalTo(3);
itemQuery.get().then((snapshot) => {
snapshot.forEach((productSnapshot) => {
console.log(productSnapshot.key, productSnapshot.child("product/id").val());
})
}).catch((error) => {
console.error(error);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.