Saqib S
Saqib S

Reputation: 541

Delete Data from Firebase server Using key or index method

I need to delete an entry from firebase using angularjs.the problem is that if I use the index then it deletes all the entries from the firebase, and if I use the key method it does nothing. Here is the code for controller.It is supposed to take key from the firebase from one of the partials.

$scope.deleteContact = function(key){
              ContactList.destroy(key);
              deleteAlert.show();
                        };

contactFactory.factory('ContactList',   function($firebaseObject,$firebaseArray){

var contact = new Firebase("https://mycontactmanager.firebaseio.com/");

This is the function to delete an entry from the firebase

destroy: function(key){
contact.remove(key);
                      }

Here is the code for partial

        <td>{{contactItem.name}}</td>
        <td>{{contactItem.email}}</td>
        <td>{{contactItem.phone}}</td>
        <td><a href="#/contact/{{key}}" class="btn btn-success btn-xs">View Contact</a>
        <button class="btn btn-danger btn-xs col-sm-offset-1" ng-click="deleteContact(key)\">Delete</button>


      </td>
      </tr>

      </tbody>         

Upvotes: 2

Views: 5080

Answers (3)

Trung Bui
Trung Bui

Reputation: 180

Since your ref is the root of your firebase database, so you need to find one child and remove that child as below

ref.child(key).$remove().then(function() {
    // Code after remove
});

Refer more here https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-remove

Upvotes: 1

amanuel2
amanuel2

Reputation: 4646

You could also use firebase method called $remove so like this:

ref.child(key).$remove();

Docs about remove located here : https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-objects.html

Upvotes: 1

David East
David East

Reputation: 32604

Remove does not take a key as a parameter.

You need to nest by calling .child(key), and then call remove.

ref.child(key).remove();

Upvotes: 3

Related Questions