0
votes

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!!

1
Add test to the $scope. What happens if you use an ng-repeat="item in test" in your view? - tommyd456
Also do a typeof on test - tommyd456

1 Answers

4
votes

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.