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();
    };
2
  • no one have answer Commented Jun 4, 2016 at 22:20
  • probably not on the weekend. Commented Jun 4, 2016 at 22:25

1 Answer 1

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.');
       }
   });
};
Sign up to request clarification or add additional context in comments.

16 Comments

not working because the employeRef is like "firebaseurl.firebaseio.com/Employee/-KJSFx_NJCdaJqusmL6S/employeeName" and i can't know the generated id
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.
there is no error it say employee deleted and there is no change in firebase
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.
First argument must be a valid firebase URL and the path can't contain ".", "#", "$", "[", or "]".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.