[英]Firebase how to find child knowing its id but not its parent's id (js)
[英]Angular & Firebase Realtime-Database. How to get child knowing its id property but without knowing its parent node
我的数据库是这样的
我可以自己进入购物车部分,甚至可以从购物车中删除产品。如果我手动输入自动生成的 ID。老实说,我已经尝试这样做 2 天了。 我研究了与 firebase 数据库相关的每个 stackoverflow 问题,但我仍然无法弄清楚。
我像这样进入购物车: firebase.database().ref('/' + account[0] + '_user' + '/cart')
但是我不能进一步 go ..假设我还提供需要删除的产品,您可以通过item.id
访问其 ID
现在,如果您单击删除按钮,整个购物车将被删除。
如果您知道要删除的节点的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.