从Firebase数据库中删除用户

时间:2016-07-07 14:27:35

标签: javascript angularjs firebase firebase-realtime-database

我开始了解有关Firebase的更多信息,并尝试创建一个简单的数据库。

我按照网站上的所有步骤操作,并成功添加了数据库中的成员。

但是......现在,了解如何删除用户是必要的。

以下是添加和删除用户的代码:

<!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>

删除用户功能的问题在哪里?

谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

首先removeUser未在任何地方定义。其次,我不认为你在ref中使用了正确的remove(),你应该使用usersRef

尝试使用user.remove()方法。

来自the User API参考:

  

删除并注销用户。

     

重要提示:这是一项需要安全性的安全敏感操作   用户最近已登录。如果未满足此要求,请询问   用户再次进行身份验证然后调用   firebase.User#重新认证。

如果要删除与用户对应的数据库节点,可以执行以下操作:

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