0

I'm trying to understand how to remove an item from my Firebase. I've set up a function (saveEmployee) to create an item , but can't figure out how to go about removing an item.

HTML

<tbody ng-repeat="employee in employees"> 
            <tr>
                <td>{{employee.employeeName}}</td>
                <td>{{employee.employeeAge}}</td>
                <td><a class="btn btn-danger" ng-click="removeEmployee(employee.employeeName)" >Delete</a></td>
            </tr>
        </tbody>

JS

$scope.removeEmployee = function(employeeName) {
        console.log(employeeName);
        $scope.myData.child(employeeName).remove();
    };
Semah Mhamdi
  • 160
  • 1
  • 10

1 Answers1

1

Use Firebase.set() and pass in null. This will delete the employee.

$scope.removeEmployee = function(employeeId) {
   var employeeRef = new Firebase('https://myfirebaseurl.firebaseio.com/employees/' + employeeId);
   employeeRef.set(null);
};

or with a callback handler.

$scope.removeEmployee = function(employeeId) {
   var employeeRef = new Firebase('https://myfirebaseurl.firebaseio.com/employees/' + employeeId);
   employeeRef.set(null, function(error) {
       if (error) {
           console.log(error);
       } else {
           console.log('Employee deleted.');
       }
   });
};
Gregg
  • 629
  • 5
  • 17
  • not working because the employeRef is like "firebaseurl.firebaseio.com/Employee/-KJSFx_NJCdaJqusmL6S/employeeName" and i can't know the generated id – Semah Mhamdi Jun 04 '16 at 22:36
  • You cant know the generated id, why not? Once you create the new record in firebase, the generated ID is passed back via the ref. – Gregg Jun 04 '16 at 22:39
  • there is no error it say employee deleted and there is no change in firebase – Semah Mhamdi Jun 04 '16 at 22:40
  • Are you using the firebase dashboard to monitor the changes? If so, you will not see updates when at the root level and have nodes expanded. The new firebase dashboard requires you to click into a child node to see the updates in real time. Not sure why this is the case. Or at least this is the case for me. For example, click into the employees node and see if the record still exists. If so try deleting it again to see if it is removed. – Gregg Jun 04 '16 at 22:43
  • First argument must be a valid firebase URL and the path can't contain ".", "#", "$", "[", or "]". – Semah Mhamdi Jun 04 '16 at 22:49
  • I recommend changing the employeeName to employeeId (the firebase auto-generated key). I have updated the example to reflect this. What is the employees object? Is it an array or object hash of data from firebase? – Gregg Jun 04 '16 at 22:50
  • myfirebaseurl.firebaseio.com/Employee/-KJSFx_NJCdaJqusmL6S/employeeName – Semah Mhamdi Jun 04 '16 at 22:54
  • Are you trying to delete employee with firebase key -KJSFx_NJCdaJqusmL6S? – Gregg Jun 04 '16 at 22:58
  • yes i want to delete with firebase key ,or how to delete ?? – Semah Mhamdi Jun 04 '16 at 23:00
  • The firebase URL you posted will only delete the employeeName. This will keep the employee record in firebase. Use this url: `myfirebaseurl.firebaseio.com/Employee/-KJSFx_NJCdaJqusmL6S` – Gregg Jun 04 '16 at 23:01
  • yes but the problem how to get the firebase key ,do you understand my problem ?? – Semah Mhamdi Jun 04 '16 at 23:02
  • Yes, I understand. The firebase key is available to you when you [create](https://www.firebase.com/docs/web/api/firebase/push.html) a new record and when you perform basic queries against firebase. – Gregg Jun 04 '16 at 23:05
  • do you have a solution ,do you tried firebase with javascript can you give me snippet of code – Semah Mhamdi Jun 04 '16 at 23:07
  • Yes, this question has been asked and can be viewed here: http://stackoverflow.com/a/16637475/5304361. Keep in mind snapshot.name() is replaced with snapshot.key(). `var employeeRef = myDataRef.push(...); var employeeId = employeeRef.key();` – Gregg Jun 04 '16 at 23:15
  • Have you made progress deleting a record? – Gregg Jun 05 '16 at 02:15
  • i turned to use angularFire is more easy than using firebase for javascript ,i maked all crud operation thanks Gregg – Semah Mhamdi Jun 05 '16 at 13:00