我要完成的是完全删除整个Firebase数据库查询结果,而不必逐个删除所有记录。 我已经在deleting和queries以及the most similar question on SO i found上阅读了相关文档,但其中没有一个是有用的。
这是我在Firebase云功能上的代码:
const relatedElementsRef = databseRef.child('related_elements');
exports.onElementDeleted = functions.database
.ref('elements/{user_id}/{element_id}')
.onDelete(function (event) {
var deletedElement = event.data.previous.val();
const relatedElementsQuery = relatedElementsRef
.child(event.params.user_id)
.orderByChild('element_id')
.equalTo(event.data.key)
relatedElementsQuery.on("child_added", (relatedElementSnap, prevChildKey) => {
relatedElementSnap.ref
.remove()
.then(()) => {
console.log("Related element deleted");
});
})
});
我想获得的是类似的东西:
//above code identical
relatedElementsRef
.child(event.params.user_id)
.orderByChild('element_id')
.equalTo(event.data.key)
.remove()
.then(() => {
console.log("Related elements deleted");
});
//below code identical
下面是数据库的简化结构:
{
"elements" : {
"user_id" : {
"element_id" : {
"name" : "This is an element",
...
},
...
},
...
},
...
"related_elements": {
"user_id" : {
"related_element_id" : {
"element_id" : "element_id", //Key of the object above
"name" : "This is a related element",
...
},
...
},
...
},
...
}