最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase -- Bulk delete child nodes - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 28, 2016 at 19:22 Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges asked Jun 28, 2016 at 19:15 user5415371user5415371 3
  • done is a bollean value inside the item branch right? – adolfosrs Commented Jun 28, 2016 at 19:22
  • what version of firebase are you running? I'm asking because react-fire doesn't support the latest changes to FB API. – U r s u s Commented Jun 28, 2016 at 19:24
  • @adolfosrs yes it is @U r s u s, I am using firebase 3.0.5 and reactfire 1.0.0 – user5415371 Commented Jun 28, 2016 at 19:32
Add a comment  | 

1 Answer 1

Reset to default 22

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);
发布评论

评论列表(0)

  1. 暂无评论