Angular & Firebase Realtime-Database。如何让 child 知道它的 id 属性 但不知道它的 parent 节点
Angular & Firebase Realtime-Database. How to get child knowing its id property but without knowing its parent node
我的数据库是这样的
我可以自己进入购物车部分,甚至可以从购物车中删除产品。如果我手动输入自动生成的 ID。老实说,我已经尝试这样做 2 天了。我研究了与 firebase 数据库相关的每个 Whosebug 问题,但我仍然无法弄清楚。
我是这样进入购物车的:firebase.database().ref('/' + account[0] + '_user' + '/cart')
但我不能更进一步..假设我也提供需要删除的产品,你可以通过 item.id
访问它的 ID
现在,如果您点击删除按钮,整个购物车都会被删除。
如果您知道要删除的节点的 product/id
属性,您可以使用查询 order/filter 节点并找到您想要的节点的键删除:
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);
})
我的数据库是这样的
我可以自己进入购物车部分,甚至可以从购物车中删除产品。如果我手动输入自动生成的 ID。老实说,我已经尝试这样做 2 天了。我研究了与 firebase 数据库相关的每个 Whosebug 问题,但我仍然无法弄清楚。
我是这样进入购物车的:firebase.database().ref('/' + account[0] + '_user' + '/cart')
但我不能更进一步..假设我也提供需要删除的产品,你可以通过 item.id
现在,如果您点击删除按钮,整个购物车都会被删除。
如果您知道要删除的节点的 product/id
属性,您可以使用查询 order/filter 节点并找到您想要的节点的键删除:
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);
})