user5009455
user5009455

Reputation:

Firebase remove() entire nested array in database

How does one delete an entire nested child from a Firebase database by referencing an entry to find the desired point of deletion?

For example, I have two entries nested under (list). (1) -KcxacywN4EkvwAzugfV and (2) -KcxaeBAIW-WgLAsajvV. I want to remove (2) -KcxaeBAIW-WgLAsajvV from the database using it's ID -KcxaeBAIW-WgLAsajvU-4-725391765511696 (See picture below).

enter image description here

I have a button setup for each database entry, to display a remove button. Each button, contains the data- or ID for each database entry.

rootRef.on("child_added", snap => {
    var title = snap.child("title").val();
    var link = snap.child("link").val();
    var type = snap.child("type").val();
    var id = snap.child("id").val();
      $("#table_data").append("<div class='col-lg-4'><div class='card card-app'><div class='card-block'><h4 class='card-title'>"+ title +"</h4><small>"+ type +"</small><hr><a href='"+ link +"' target='_blank'>Download</a>           <a class='user float-right' onclick='removeClick()' data-name='"+ id +"'>Remove</a>            </div></div></div>");
  });

The onClick event and associated id for the button, trigger this function. My mental idea, is to then take the ID from data- to delete its nested child, (2) -KcxaeBAIW-WgLAsajvV from the database. Same process for all other nested entries.

function removeClick() {
  $(".user").click(function() {
        rvm = $(this).attr("data-name");
    });
  alert(rvm);
  firebaseRef.remove(rvm);
}

I've studied https://firebase.google.com/docs/database/web/read-and-write and can't seem to figure out the actual deletion of the nested entry. How can I use remove() or maybe another method to accomplish this?

I have been trying this, to get a better understanding.

firebaseRef.child(rvm).remove();

Since rootRef, is how I'm viewing data. I tried.

rootRef.child().remove();

This simply deletes the whole database...

Final running code:

function removeClick() {
  $(".user").click(function() {
        rvm = $(this).attr("data-name");
  });
  alert(rvm);
  var query = rootRef.orderByChild("id").equalTo(rvm);
  query.once("value", function(snapshot) {
   snapshot.forEach(function(itemSnapshot) {
       itemSnapshot.ref.remove();
   });
 });
}

Upvotes: 3

Views: 3148

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599581

You can only remove an item if you know its key. So given your current structure, you will first need to look up the key for the id you mention.

Assuming you have a variable ref that points to the root of the database:

var listRef = ref.child("list");
var query = listRef.orderByChild("id").equalTo("-KcxaeBAIW-WgLAsajvU-4-725391765511696");
query.once("value", function(snapshot) {
   snapshot.forEach(function(itemSnapshot) {
       itemSnapshot.ref.remove();
   }); 
});

If you have only a single child with the id, you should consider restructuring your database to use the id as the key. If you do that, you could remove the item with:

listRef.child("-KcxaeBAIW-WgLAsajvU-4-725391765511696").remove()

Upvotes: 3

Related Questions