简体   繁体   中英

Firebase — Bulk delete child nodes

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 ie 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM