【问题标题】:angularjs firebase onAuthStateChanged signout the database errorangularjs firebase onAuthStateChanged 注销数据库错误
【发布时间】:2017-06-15 02:44:48
【问题描述】:

当我退出应用程序时,我收到此错误:

Error: permission_denied at /users/KuehJHDJ15SfS1s6FIh1: Client doesn't have permission to access the desired data.

 at G (firebase.js:432)
    at Object.G (firebase.js:456)
    at Pg (firebase.js:398)
    at Eg.g.wd (firebase.js:391)
    at sg.wd (firebase.js:381)
    at bg.hg (firebase.js:379)
    at eg (firebase.js:363)
    at WebSocket.La.onmessage (firebase.js:362)

我怀疑我的问题与这里非常相关: Refs not rerunning after onAuthStateChanged changes Firebase 3.0.0

这是我的安全规则:

{
  "rules": {
    "users": {
      ".read": "auth != null && root.child('hashlist/'+root.child('users/'+auth.uid+'/hash').val()).val() == auth.email",
      ".write": "auth != null"
    },
    "hashlist": {
      ".read": true,
      ".write": "auth != null && root.child('users/'+auth.uid+'/role').val() == 'admin'"

    }
  }
}

感谢您的帮助,谢谢。

【问题讨论】:

  • 是的。这是相关的,但恰恰相反:您需要在退出之前在监听参考上调用off
  • @cartant 我应该怎么做?可以举个例子吗?
  • @cartant 我如何找到我的听力参考?因为我现在有大约 50 个......

标签: angularjs firebase firebase-realtime-database firebase-authentication


【解决方案1】:

抛出错误是因为有 ref 在退出后继续侦听更改,并且一旦退出,安全规则就会阻止它们这样做。

为防止出现错误,您需要在退出前对正在监听的任何 ref 调用 off

没有自动化的设施可以做到这一点;这是你必须自己管理的事情。你应该:

  • 当不再需要时,使用$scope.$on("$destroy", ...) 关闭作用域中的监听 refs(我已经很久没有使用 Angular JS,所以我的记忆在这里可能有点不可靠)。
  • 在只需要读取一次值的情况下,使用 once 确保不会留下悬空的侦听器。
  • 考虑使用AngularFire,它为对象和数组提供了一些很好的绑定,并且会自行连接(如果我没记错的话),以便在作用域被销毁时关闭其内部监听引用。

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 2018-06-22
    • 1970-01-01
    • 2021-12-13
    • 2018-04-30
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2016-11-14
    相关资源
    最近更新 更多