Zaid Mirza Zaid Mirza - 5 months ago 80
AngularJS Question

Retrieving child records from firebase using unique auto-generated keys

Scenario is,

Our data on firebase is structured as shown below

enter image description here

Image is based on sample database where post1,post2 under Posts node are randomly generated keys just like "YyhsyyHyzhh-Ke" etc and comment1,comment2 also randomly generated keys.

My problem is that I want to fetch all posts and related comments from firebase and as you can see under post1,there is a comments node which contains keys of comments related to post1 but im unable to get keys of comments so that I can browse to that comment's text

see below code

appMainModule.controller('GetCtrl', ['$scope', '$firebase', function ($scope, $firebase) {

var firebaseObj = new Firebase("FirebaseURL/Posts");

var sync = $firebase(firebaseObj);

$scope.articles.$loaded(function (data) {
var mainVar ;
for (var i = 0; i < data.length; i++) {
mainVar = data[i];
var commentsWithIds=mainVar.Comments;
//how to fetch keys side of data from commentsWithIds as I dont know rendomly generated keys


I want get comments keys so that I cloud make URL like this new Firebase("FirebaseURL/Comments"+CommentsAutoGeneratedKey) .


If I understood your question well, you can use Object.keys() method:

var comments = {  
    "text":"this is first comment"
    "text":"this is 2nd comment"
    "text":"this is third comment"