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).
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();
});
});
}
