Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a Firebase structure like this:

user {
  uid {
    Lessons {
      lid1 {
         Title: ...
          }
      lid2 {
         Title: ...
          }
     }
   }
  }

I want to use AngularFire to convert user as array so I can filter them using Angular like this:

var usersRef = new Firebase($rootScope.baseUrl + "users"); 
var userListfb = $firebase(usersRef).$asArray();

The problem is, I also need the number of child of the Lessons object. When I log the userListfb, it is an array. But inside the array, the Lessons node still an object. I can not user length to get its length. What is the correct way to find out the number of child of the Lessons Node with Firebase AngularFire?

Edit 1 According to Frank solution, I got an infinite loop (digest circle error from AngularJS). The problem is, I will not know the "uid" key. I need to loop it in the first array to get the uid into the second firebaseArray. Let's say I have a ng-repeat="user in users" in the view and call this on view level in each repeat:

{{getLessonLength(user.uid)}}

Then in the controller, I have this function:

$scope.users = $firebaseArray($scope.usersRef);

$scope.getLessonLength = function (uid) {

        var userRef = $rootScope.baseUrl + "users/" + uid + "/lessons/";

        var lessonsNode = $firebaseArray(new Firebase(userRef));

        return lessonsNode.length;
    }
}

And it throw this error: Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []

All I want it is something like var lessonsCount = snapshot.child('lessons').numChildren() in regular Firebase .on('child_added' ...), the numChildren() function in FirebaseArray. Please help!

share|improve this question
2  
AngularFire docs: "AngularFire is not a wrapper on the entire Firebase API with $ signs in front of the methods. It is also not ideal for synchronizing deeply nested collections inside of collections. But deeply nested collections should typically be avoided in distributed systems." – Kato Jan 27 at 20:25
    
@Kato So you suggestion I should not put the lessons object array inside the user array? So how should I flatten this structure in the Firebase way? I feel like nested array is so hard to avoid. Is there a different kind of thinking? Could you point me into the right direction? – Hugh Hou May 17 at 19:46

1 Answer 1

up vote 1 down vote accepted

AngularFire contains quite some code to ensure that an ordered collection in your Firebase maps correctly to a JavaScript array as Angular (and you) expect it.

If you have a reference to a specific user, you can just create a new sync ($firebase) and call $asArray on that.

var usersRef = new Firebase($rootScope.baseUrl + "users"); 
var userListfb = $firebase(usersRef).$asArray();
var uid1LessonsRef = userRef.child('uid1').child('Lessons');
var uid1LessonsArray = $firebase(uid1LessonsRef).$asArray();
uid1LessonsArray.$loaded().then(function(arr) {
    console.log('Loaded lessons, count: '+arr.length);
});

The data will only be synchronized once, no matter how many references you create to it.

share|improve this answer
    
But my problem is I need to connect the userListfb array to the Lesson array. I want the lessons number (the array length) and insert the value as a property inside the userListfb array. So I can use ng-repeat to go tho the userListfb array. When I try to use ng-repeat to get the uid number and call a function inside an expression like this {{calculateNum(list.uid)}}, the calculateNum will create an infinit loop error... I don't think it is ideal as well to let the loop open an new connection to firebase in each loop to just get the length of an inside object... – Hugh Hou Jan 27 at 1:31
    
Franks answer directly addresses your problem. You'll want to create a synchronized array for the lessons, specifically, if you expect them to appear in an array. – Kato Jan 27 at 20:26
1  
I want the lessons to be a nested array inside the user array. But firebase only return the first level array, lessons node is still an object... – Hugh Hou Jan 30 at 0:44
    
Also, could you clarify what you mean "The data will only be synchronized once, no matter how many references you create to it."? – Hugh Hou May 18 at 3:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.