Ozi Ozi - 26 days ago 18
AngularJS Question

How can I get all objects in Firebase with using AngularJs

I want to get all objects from my firebase database but I am getting error. Here is how I tried so far;

My Firebase DB (Json);

{
"posts" : {
"-KVWh7aWrTG04ZdtbTKD" : {
"postText" : "heo1"
},
"-KVWhAC2SkRC_fY75a2I" : {
"postText" : "heo2"
}
}


And here is my controller;

$scope.loadPost = function() {
$scope.posts=$firebaseArray(fireBaseData.refpost());
}


and my service;

.factory('fireBaseData', function($firebase) {
var refPost = new Firebase("https://myapp.firebaseio.com/posts");
refPost: function() {
return refPost;
}
}
})


Result: Error: fireBaseData.refpost is not a function

What was wrong any suggestion ? Thanks

Answer

Always prefer the documentation instead tutorials/blogs/sites...

https://firebase.google.com/docs/database/web/start

First you need initialize Firebase SDK with a code snippet in the console.

Then you you create a reference for the father of 'posts' node, like this.

      var Fatherposts = firebase.database().ref().child('posts');
        Fatherposts.on('value', function(snapshot){

          //Finally you get the 'posts' node and send to scope
          $scope.Fatherposts = snapshot.val().posts;

        });

Go to documentation and read about function 'on' and 'once' https://firebase.google.com/docs/database/web/read-and-write

Comments