Diaconu Eduard Diaconu Eduard - 5 months ago 19
AngularJS Question

Remove users from Firebase

I start learn more about Firebase and i try to create a simple database.

I follow all steps from website and i successfully added members in database.

But...now, it's necessary to learn how can remove an user.

Here is my code to add and remove users:

<!DOCTYPE html>
<html>
<head>


</head>
<body>
<button onclick="saveData()">Save Data</button>
<button onclick="printData()">Print Data</button>
<button onclick="printData2()">Print Data2</button>
<button onclick="remove()">Remove</button>
<script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
<script>
var ref = new Firebase("https://projecttest-9aee9.firebaseio.com/web/saving-data/fireblog");
var usersRef = ref.child("users");
function saveData(){
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
}
function printData(){

usersRef.on("value", function(snapshot) {
console.log(snapshot.val());
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
function printData2(){

ref.child("users/gracehop/date_of_birth").on("value", function(snapshot) {
console.log(snapshot.val());//"December 9, 1906"
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}


function remove(){
ref.removeUser({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Grace Hopper"
}

});
}
</script>
</body>
</html>


Where is the problem with remove users function?

Thank you for help!

Answer

First removeUser is not defined anywhere. Second, I don't think you're using the correct ref in remove(), you should use usersRef.

Try using the user.remove() method.

From the User API reference:

Deletes and signs out the user.

Important: this is a security sensitive operation that requires the user to have recently signed in. If this requirement isn't met, ask the user to authenticate again and then call firebase.User#reauthenticate.

If you want to delete the database node corresponding to a user, you could do something like this:

usersRef.remove()
  .then(function() {
    console.log("Remove succeeded.")
  })
  .catch(function(error) {
    console.log("Remove failed: " + error.message)
  });