【问题标题】:Remove users from Firebase database从 Firebase 数据库中删除用户
【发布时间】:2016-07-07 14:27:34
【问题描述】:

我开始了解有关 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>

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

感谢您的帮助!

【问题讨论】:

    标签: javascript angularjs firebase firebase-realtime-database


    【解决方案1】:

    第一个removeUser 没有在任何地方定义。其次,我认为您在remove() 中使用的ref 不正确,您应该使用usersRef

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

    来自the User API参考:

    删除并注销用户。

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

    如果你想删除一个用户对应的数据库节点,你可以这样做:

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

    【讨论】:

    • var ref = new Firebase("projecttest-9aee9.firebaseio.com/web/saving-data/fireblog"); ref.remove({ alanisawesome: { date_of_birth: "June 23, 1912", full_name: "Alan Turing" } });
    • 也许......类似的东西?谢谢帮助!
    • 就是这样!再次感谢你!祝你有美好的一天:)
    • 没问题,乐于助人
    猜你喜欢
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多