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

2
  • Add test to the $scope. What happens if you use an ng-repeat="item in test" in your view? Commented Oct 16, 2014 at 9:27
  • Also do a typeof on test Commented Oct 16, 2014 at 9:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

AJAX why do you trouble the new programmers so? I bite my thumb at thee, asynchronous villain. I bite my thumb at thee!
Yup. And unfortunately it seems best understood when explained in the OP's context, instead of referring to a standard answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.