Firebase and AngularFire - $ add on array - unexpected behavior
dayPath = ref.path.toString() + '/' + configId + '/screens/' + screenIndex + '/days/',
// the ref for the days object
daysRef = fbutil.ref(dayPath),
// the sync for the days object
daysSync = fbutil.syncObjectReference(daysRef);
// the collection as a $firebase array
var list = daysSync.$asArray(),
items = [],
number;
console.log(list);
list.$add({dummy: 'Test'});
According to the documentation, when I use $ add with $ asArray, $ add should do "push". But instead, it creates a hash key instead of a numeric index.
So, bogus: test has a parent containing a hash key. The expected is a numeric index, I mean: an array element.
Can anyone help me? I just have 1 week of experience with this database.
The result is one ...
screens
...0
.......days
..........0
..........1
..........2
.........-JrT5ZATDELIR3gXAvah
................dummy: test
source to share
AngulareFire is built on the Firebase JavaScript SDK. So when the AngularFire documentation says what it uses push
internally, it's not JavaScript Array.push
, but Firebase push
operation . And Firebase push
generates its own keys, it doesn't generate regular array indices.
The reason for this is best explained in the Firebase documentation on arrays , but it essentially boils down to: Arrays don't work well in distributed environments because all clients must agree array.length
to be able to add a new item.
So, it $firebaseArray.$add
will create a so called push identifier. They are ordered like array indices, but can be generated via clients without the risk of conflicts.
I noticed that you are on an older version of AngularFire. I highly recommend that you follow the "official" AngularFire Quickstart and Guide .
source to share