There are two problems here:
- Your query doesn't match the nodes you want delete.
- Firebase doesn't support delete-queries.
Your query doesn't match the nodes you want delete
Your query starts at a node products/product, which doesn't exist in your database, and then orders/filters the id property of each child node.
What you want instead is to start at the products node, and then order/filter on its product/id property of each child node:
In code:
const userProductRef = db.ref(`products`);
var query = userProductRef.orderByChild('product/id').equalTo(id);
Don't forget that you'll need to define an index for product/id for this on the products level in your rules.
Firebase doesn't support delete-queries
You can't simply pass the query and delete instruction to Firebase. To write a node in the database, you'll need to know the entire path to that node.
So you'll need to:
- Execute the a query.
- Loop over the results in your application code.
- Delete each matching node individually or in a multi-path update.
With the query above, that'd be:
query.once("value").then((snapshot) => {
snapshot.forEach((child) => {
child.ref.remove();
});
})
Or with a multi-path/batch update:
query.once("value").then((snapshot) => {
let updates = {};
snapshot.forEach((child) => {
updates[child.key] = null;
});
ref.update(updates);
})