0

I am aware that this question has been answered a few times but I am not satisfied with any of those solutions. In this example the final structure doesn't really look like array.

My solution was to get the data "array" from FireBase and then use the lenght of the data as a key. Here is the code:

           var objectToSave = {};
            objectToSave[Object.keys(this.officeArray).length] = officeID
            this.$.user_office_document.ref.update(objectToSave);

This solution works but it requires fetching the data which hopefully shouldn't be necessary.

Thanks

3
  • There are very good reasons why Firebase doesn't use arrays for its collections. The need to have a synchronized copy of the data in order to be able to append is one of them. I recommend reading Best Practices: Arrays in Firebase and then switching over to the way the Firebase documentation treats lists Commented Dec 15, 2016 at 16:53
  • Ok, I realized I might run into synchronization issues. I simple need to store a set of unique values (firebase ids) and I don't care about the index. I guess I'll just use {value: true} and iterate over the keys when needed. Thanks Commented Dec 15, 2016 at 17:06
  • 1
    The value: true pattern is indeed perfect for that. It's how you model a set in Firebase (given that there must always be a value for each key). Commented Dec 15, 2016 at 18:14

0

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.