1

Am in the angular-fire-seed tutorial stage and experimenting with messages and child posts, for some bizarre reason I cannot see the children when I explicitly try to display them, but can see it when expanding on the parent node in the console. These messages display properly in the html where I have the ng-repeat, so I know I am getting the messages at least, albeit childless.

I thought angularfire-seed utils might be chopping or slicing some children so I reverted to straight firebase

This is what I have:

Code:
-----
var url = fbutil.ref() + "/messages/";
var ref = new Firebase(url);

var sync = $firebase(ref).$asArray();

console.log(sync);          //this I can see as a proper $firebaseArray object
console.log(sync.length);   //this displays as 0 even though length is 3 in object above
console.log(sync[1]);       //displays as undefined 

Data:
----
messages/id1/text
        /id2/text
        /id2/post
        /id3/text

Thanks in advance for pointing out what I am mis-assuming !

killjoy
  • 790
  • 1
  • 9
  • 16

2 Answers2

2

You seem to be falling for the common asynchonisity problem.

This is your code fragment:

var sync = $firebase(ref).$asArray();

console.log(sync);        //this I can see as a proper $firebaseArray object
console.log(sync.length); //this displays as 0 even though length is 3 in object above
console.log(sync[1]);     //displays as undefined 

The call to $asArray() doesn't return a Firebase array immediately, since the data may take some time to come down from the Firebase servers. So instead it returns the promise of a Firebase array, something that in the future will contain your data.

By the time you inspect the out of console.log(sync) in the console, the data will have downloaded so it is (as you say) a proper synchronized array. But when the console.log(sync.length) line runs, the data probably hasn't downloaded yet.

You can wait for the data to be downloaded with the $loaded() method. So:

var sync = $firebase(ref).$asArray();

console.log(sync);
sync.$loaded().then(function(sync) {
  console.log(sync.length); 
  console.log(sync[1]);     
}
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
0

got the answer from AngularFire Accessing child element methods

short answer: once assigned to a JS var, it is a POJO, not a $firebase object anymore.

Community
  • 1
  • 1
killjoy
  • 790
  • 1
  • 9
  • 16