Firebase -- 批量删除子节点 [英] Firebase -- Bulk delete child nodes

查看:27
本文介绍了Firebase -- 批量删除子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 reactFire、Firebase 和 reactJS 构建一个简单的待办事项应用程序.我遇到的问题是当我尝试批量删除列表中已完成的条目时.

I am building a simple todo app using reactFire, Firebase, and reactJS. The problem I am running into is when I try to bulk delete completed entries in the list.

componentWillMount: function() {
        this.ref = Firebase.database().ref("items/");
        this.bindAsArray(this.ref, "items");
        this.ref.on('value', this.handleDataLoaded);
}

for (var i in this.state.items) {
            var key = items[i]['.key'];
            if(items[i].done){
                this.ref.child(key).remove();
            }
}

循环提前结束,即在删除所有完成的条目之前,因为渲染函数被调用.

The loop ends prematurely i.e. before deleting all the completed entries, because the render function is called.

推荐答案

您可以使用多位置更新一次性删除所有已完成的项目:

You can delete all completed items in one go by using a multi-location update:

var updates = {};
for (var i in this.state.items) {
    var key = items[i]['.key'];
    if(items[i].done){
        updates[key] = null; // setting value to null deletes the key
    }
}
this.ref.update(updates);

这篇关于Firebase -- 批量删除子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆