Issue
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.
Solution
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);
})
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.