permission_denied at /: 客户端没有访问所需数据的权限

permission_denied at /: Client doesn't have permission to access the desired data

这是我第一次使用 firebase,当我尝试检索数据时无法正常工作。

我已经实现了 firebase 的授权并使用了它,现在可以从任何控制器(检查下面的控制器)中查看用户数据,但是当我尝试从数据库中检索数据时出现此错误:

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

我的关于提要的数据库规则是:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

如果我制作它们 public 它可以工作并且我可以获取数据,但我不想那样做,而且我在网上找不到任何关于如何发送用户 ID 以获取数据的文档来自 firebase 的数据。

我的控制器:

.controller('homeCtrl', ['$scope','$firebaseObject','currentAuth',
    function($scope,$firebaseObject,currentAuth) {

        console.info('currentAuth',currentAuth); // This returns the needed info about the current user including the uid

        // This is where I get the permission denied error.
        var ref = firebase.database().ref();
        // download the data into a local object
        $scope.data = $firebaseObject(ref);
}]);

感谢任何帮助,谢谢。

当您附加侦听器时,用户似乎未通过身份验证。像您一样记录用户并不是一个很好的指标,因为浏览器的开发工具会在 currentAuth 更改时向您显示更新后的值。如果您改为记录 console.info('uid',currentAuth.uid),您可能会看到它是 null.

您应该只在用户通过身份验证后附加您的侦听器。来自 AngularFire documentation:

$scope.authObj.$onAuthStateChanged(function(firebaseUser) {
  if (firebaseUser) {
    console.log("Signed in as:", firebaseUser.uid);
    var ref = firebase.database().ref();
    $scope.data = $firebaseObject(ref);
  } else {
    console.log("Signed out");
  }
});