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:
Given that, I created the following rules:
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.
I'm not sure how you came to this construct:
But the
once
callback only takes a single argument for thevalue
event. See the relevant Firebase API documentation.So you'll have to change it to:
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.