简体   繁体   中英

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. I researched every stackoverflow question related to firebase database but I still couldn't figure it out.

I get to cart like this: 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

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:

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);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM