3
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 with the documentation, when I use $add with $asArray, the $add supposed to do a "push". But instead it's creating a hash key instead a numeric index.

So, the dummy: test has a parent containing a hash key. The expected is a numeric index, I mean : array item.

Can someone give me some help? I just have 1 week of experience in this database.

The result is this one...

 screens
 ...0
 .......days  
 ..........0
 ..........1
 ..........2
 .........-JrT5ZATDELIR3gXAvah
 ................dummy: test

2 Answers 2

2

AngulareFire is built on the Firebase JavaScript SDK. So when AngularFire's documentation says it uses push internally, it is not referring to JavaScript's Array.push, but to Firebase's push operation. And Firebase's push generates its own keys, it does not generate regular array indexes.

The reason for that is best explained in Firebase's documentation on arrays, but essentially boils down to: arrays don't work well in distributed environments, because all clients have to agree on the array.length in order to be able to add a new item.

So $firebaseArray.$add will generated a so-called push ID. They are ordered, like array indexes, but can be generated across clients without risk of conflicts.

I noticed that you're on a somewhat older version of AngularFire. I highly recommend that you follow the "official" quickstart and guide for AngularFire.

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

1 Comment

Ty, Frank ! I updated the post for a better visualisation. It's now contains the result. The 0,1,2 is current records...I'm trying to add a key#3 like a array. But instead...It's create a hash key and put my dummy object as child. I'm trying to say : the records in array already exists. I'm just trying to "continue" this fashion by adding more records. Looks like the arrays has been created before in some how. What is the best way to continue to add records for this already existing array ?
0

I would like to comment but i don't have enough reputation yet so i'm doing it here.

The solution in my eyes is very simple:

Instead of:

list.$add({dummy: 'Test'});

Do:

list[index] = {dummy: 'Test'};

2 Comments

Hi André. This is the anti-pattern that should be avoided. Check out the AngularFire guide section, Saving Lists of Data, where you can see that it is clearly marked as an anti-pattern. Take a look at the blog post, Best Practices: Arrays in Firebase. This contradicts Frank's answer above that explains why using regular array indices should be avoided.
Completely true. But I wasn't trying to tell what he should be doing instead i was answering this particular question that was asked (how can i continue the array). Without knowing the complete situation it is almost impossible to give the best way to do something.

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.