Nevershow2016
Nevershow2016

Reputation: 570

How to delete comment from Firebase and from my app?

I have created a simple comment box where someone can enter a username and a comment and it is written to the database.

What I am trying to do right now is create a cross by that comment so that the user can delete it. However I am not to sure how to do this.

Could anyone point me in the right direction, or some tutorials would be great?

Code:

fireBaseRef.on('child_added', function(snapshot) {
    // // store all current comments from firebase
    var fbData = snapshot.val()

Upvotes: 0

Views: 397

Answers (1)

Elias Meire
Elias Meire

Reputation: 1278

You need to store the keys of each comment you get from the Firebase in the child_added event handler.

fireBaseRef.on('child_added', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
        var key = childSnapshot.key();
    });
});

When a comment has to be deleted later you can do so by using it's key.

fireBaseRef.child(key).remove(function(error) {
    alert(error ? "Error" : "Success");
});  

Alternatively you can also get a reference when you push to Firebase like this.

var pushedRef = ref.push({test: "true"});

You can then delete the data at this reference.

pushedRef.remove(function(error) {
    alert(error ? "Error" : "Success");
});

Upvotes: 1

Related Questions