0

I'm using AngularFire 0.8

I called the following function to get an instance of an array.

var test = $firebase(new Firebase(URL)).$asArray();

When I console.log 'test', I get the following (see screenshot).

enter image description here

It looks like test is an array. However, when I do "console.log(test[0])", I get undefined.

Why isn't test an array with two elements like console.log(test) seems to show it should be???

Help!!

jmtoung
  • 1,002
  • 1
  • 14
  • 28

1 Answers1

4

Most likely you're calling console.log on the array before the data is loaded. To access the data in the array, you have to wait until the data is loaded.

To do that in code, you use this construct:

var test = $firebase(new Firebase(URL)).$asArray();
test.$loaded().then(function(array) {
    console.log(array[0]);
});

Update

This answer gets more upvotes than I think it's worth. When you're using AngularJS and AngularFire, it is often a bad omen when you use console.log to debug the data that is loaded.

The AngularFire documentation on handling asynchronous operations now has to say on it:

The easiest way to log the data is to print it within the view using Angular's json filter.

{{ data | json }}

AngularFire tells the Angular compiler when it has finished loading the data, so there is no need to worry about when it be available.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    AJAX why do you trouble the new programmers so? I bite my thumb at thee, asynchronous villain. I bite my thumb at thee! – Kato Oct 16 '14 at 21:05
  • 1
    Yup. And unfortunately it seems best understood when explained in the OP's context, instead of referring to a standard answer. – Frank van Puffelen Oct 16 '14 at 21:23