简体   繁体   English

Angular & Firebase 实时数据库。 如何让孩子知道其 id 属性但不知道其父节点

[英]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.

相关问题 Firebase如何找到知道其ID但不知道其父母ID(JS)的孩子 - Firebase how to find child knowing its id but not its parent's id (js) 有没有办法在不知道其类名或ID的情况下选择父div? - Is there a way to select parent div without knowing its class name or ID ? 在不知道父元素的情况下删除dom元素? - Remove dom element without knowing its parent? 如何仅使用 Vanilla Javascript 在不知道其位置的情况下定位子节点 - How to Target a Child Node Without Knowing Its Position Using Only Vanilla Javascript 获取内容 <div> 知道它的身份 - Get content of <div> knowing its id 如何在不知道Java中ID的情况下获取输入文本字段的值 - How to get value of input text field without knowing its id in Javascript 如何在不知道键值的情况下获取枚举值? - How to get enum values without knowing its keys? 如何在不知道名称的情况下访问对象 - How to access an object without knowing its name Firebase:如何在不知道其唯一ID的情况下更新记录的值项? - Firebase: HowTo update the value item of a record without knowing its unique id? GoJS在不知道父节点密钥的情况下删除子节点 - GoJS delete child nodes without knowing parent node's key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM