For loop in an $firebaseArray

408 views Asked by At

I found some similar topics, but most of them were outdated because I'm using angularfire 1.1.1.

I had an AngularJS app creating markers on a Google map. The data was stored in external JSON files and it worked just fine when I turned it into arrays. When I put the data into an firebase database it stopped working. The ng directives with the data works just fine, but when accessing the array data data in the js code it does not really work.

    app.controller('MapCtrl', ['$scope', '$firebaseArray', function($scope, $firebaseArray){

var ref = new Firebase("https://radiant-inferno-6439.firebaseio.com/tracks");
var syncObject = $firebaseArray(ref);
    ...

With the old offline JSON version logging the syncObject looked like this:

    [Object, Object, Object]
    0: Object
    1: Object
    2: Object
    length: 3
    __proto__: Array[0]

But now it looks like this:

    []
    0: Object
    1: Object
    2: Object
    $$added: () { [native code] }
    $$error: () { [native code] }
    $$getKey: () { [native code] }
    $$moved: () { [native code] }
    $$notify: () { [native code] }
    $$process: () { [native code] }
    $$removed: () { [native code] }
    $$updated: () { [native code] }
    $add: () { [native code] }
    $destroy: () { [native code] }
    $getRecord: () { [native code] }
    $indexFor: () { [native code] }
    $keyAt: () { [native code] }
    $loaded: () { [native code] }
    $ref: () { [native code] }
    $remove: () { [native code] }
    $save: () { [native code] }
    $watch: () { [native code] }
    length: 3
    __proto__: Array[0]

Why is it like that? When I print the length of the array it says 0. I tried the $loaded function but it does not affect.

    syncObject.$loaded().then(function(syncObject) {
 console.log(syncObject.length);
    });

Neither can I access the properties of the array, so it is impossible to loop through it.

1

There are 1 answers

0
Snellman On

Looks like the $loaded() method isn't working. I added a 2 second timeout between loading the data and doing anything else, so it started to work.