1

we inherited a old database structure w/c stores colors as arrays in firebase realtime database

enter image description here

As you can see from the figure, index 5 is missing since it was set to null and firebase don't store null values

The problem with this is as you can see, index number 5 is missing since firebase doesn't store the values of array elements with null values.

For example: colorCodes[5] = null;

The problem is when we fetch the colors, it returns an array value and loses it's correct index information. Let's say i store the fetched colors in colors array and looking at colors[5] would then return a value since firebase just returns a simple array NOT containing the original indexes.

Is there a way to return a object instead of array so it still points to the correct indexes so the color on index 5 would still return null.

1 Answer 1

2

That's the intended behavior as explained in this blog.

Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.

// we send this
['hello', 'world']
// Firebase stores this
{0: 'hello', 1: 'world'}

So with your current structure you'll get an array:

["#D1FF61", "B9BEFF", ...]

The order will be same as in the database.

If you change your structure so each index stores an object like this:

enter image description here

This should return an array with both item index and value:

enter image description here

Do note that index 2 is empty as we have node in the response as well so it should be easier for your to process your array.

Also note that,

// since the keys are numeric and sequential, // if we query the data, we get this ['a', 'b', 'c', 'd', 'e']

// however, if we then delete a, b, and d, // they are no longer mostly sequential, so // we do not get back an array {2: 'c', 4: 'e'}

To ease things, I would rather store a map instead of array with keys hex1, hex2 and so on. Object with your expected structure can easily be formed using Javascript. If you still want to have arrays itself, then you should take a look at Firestore.

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

6 Comments

unfortunately, we have a very specific use case where we need to get the object representation of the array field. Is there a workaround for this?
@The.Wolfgang.Grimmer can you share a sample object that you are expecting?
basically the array is converted into { "0": "hex1", "1": "hex2", "4": "hex4" } where the original index is respected
@The.Wolfgang.Grimmer as mentioned the index will be same in the retrieved array so hex1 will be at 0 and so on ... You can loop through the array arr.forEach((hex, i) => myMap[i] = hex) to get the object. But read the blog mentioned in answer, if you remove any key and it's no longer sequential it'll end up in an object instead of array.
@The.Wolfgang.Grimmer as mentioned the index will be same in the retrieved array so hex1 will be at 0 and so on ... You can loop through the array arr.forEach((hex, i) => myMap[i] = hex) to get the object. But read the blog mentioned in answer, if you remove any key and it's no longer sequential it'll end up in an object instead of array.
|

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.