本文介绍了/的Permission_denied:客户端无权访问所需的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在/处被拒绝权限:客户端无权访问所需的数据.

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

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

如果我将它们公开,则可以正常工作并且可以获取数据,但是我不想这样做,而且我也找不到在线文档,无法找到有关如何发送用户ID以从Firebase获取数据的文档./p>

我的控制器:

  .controller('homeCtrl',['$ scope','$ firebaseObject','currentAuth',函数($ scope,$ firebaseObject,currentAuth){console.info('currentAuth',currentAuth);//这将返回有关当前用户的所需信息,包括uid//这是我收到权限拒绝错误的地方.var ref = firebase.database().ref();//将数据下载到本地对象$ scope.data = $ firebaseObject(ref);}]); 

感谢您的帮助.

解决方案

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

仅在用户通过身份验证后,才应附加您的侦听器.从 AngularFire文档:

  $ scope.authObj.$ onAuthStateChanged(function(firebaseUser){如果(firebaseUser){console.log(登录为:",firebaseUser.uid);var ref = firebase.database().ref();$ scope.data = $ firebaseObject(ref);} 别的 {console.log(登出");}}); 

This is my first time using firebase, and when I try to retrieve data I can't get it to work.

I've implemented the firebase's authorisation and used it and can now see the user's data from any controller (Check controller below), but when I try to retrieve data from the database I get this error:

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

My database rules regarding the feed are:

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

If I make them public it works and I get data, but I don't want to do that, and I can't find any documentation online about how do I send the user id to get the data from firebase.

My controller:

.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);
}]);

Any help is appreciated, thanks.

解决方案

It looks like the user isn't authenticated when you attach the listener. Logging the user like you do is not a good indicator for this, since the dev tools of your browser will show you the updated value of currentAuth as it changes. If you instead log console.info('uid',currentAuth.uid) you will likely see that it's null.

You should only attach your listeners once the user is authenticated. From the 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");
  }
});

这篇关于/的Permission_denied:客户端无权访问所需的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:54