Reading Firebase data is returning client error despite rule being "true"

1.6k views Asked by At

I am trying to figure out how to do a simple CRUD with angular + firebase. I started with a read operation. I have the following data structure:enter image description here

Given that, I created the following rules: enter image description here

I have a working factory as follows:

factory('wordsFactory', function($http){
  var factory = {};
  var words = [
    {content:"hi", definition:"ooo"},
    {content:"h3", definition:"ooo222"}
  ];
  factory.getWords = function(){
    return words;
    //ajax call here
  };
  factory.addWords = function(){
    //something
  }
  return factory;
})

I modified it to try and include the call like so:

factory('wordsFactory', function($http){
  var factory = {};
  var words = [];
  var ref = new Firebase('https://my-firebase.firebaseio.com/words');
    ref.once('value', function($scope, snapshot) {
        $scope.variable = snapshot.val();
        words = [
          {content: ref.content, definition: ref.definition}
        ];
  });
  factory.getWords = function(){
    return words;
    //ajax call here
  };
  factory.addWords = function(){
    //something
  }
  return factory;
})

However, whenever I try to read I get:

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

and

FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'val' of undefined

A number of things, I realize that because of the way I have it, even if it worked, it would only return 1 value. I'm ok with that right now since I just want to figure out how to make it work. I also know that my current data structure could be greatly improved, again I just did it to learn but feel free to suggest anything you think might help me.

My chief concern right now is that I can't get it to read. I should also mention that the factory works properly without firebase, it's being called in the main controller.

2

There are 2 answers

0
Frank van Puffelen On

I'm not sure how you came to this construct:

ref.once('value', function($scope, snapshot) {

But the once callback only takes a single argument for the value event. See the relevant Firebase API documentation.

So you'll have to change it to:

ref.once('value', function(snapshot) {

And find another way to get the scope in there.

For troubleshooting the failing read operation, I recommend using the simulator tab in your Firebase dashboard. It will typically show you a better explanation of why the operation is rejected. This detailed information is intentionally not shown in the regular clients.

0
test_124 On

I think your issue is related to RULES.

Try replacing the default RULES to below:

{
  "rules": {    
     ".read": true,
     ".write": true
  }
}

Now you can access without any Errors.