删除firebase查询结果

时间:2017-07-27 13:19:56

标签: firebase firebase-realtime-database google-cloud-functions

我要完成的是完全删除整个Firebase数据库查询结果,而不必逐个删除所有记录。 我已经在deletingqueries以及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",
                ...
            },
            ...
        },
        ...
    },
    ...
}

0 个答案:

没有答案