![](/img/trans.png)
[英]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.