0

I have a Firebase collection that looks like this:

screens collection

Source: https://weld-development-tom.firebaseio.com/projects/5649ddc3f709a10c003cf031/screens (need auth)

But when I request it over the Firebase JSON API, it looks like this:

[
  null,
  {
    "dateUpdated" : 1454489265605,
    "elements" : { /* removed to make question shorter */ },
    "name" : "Screen 1",
    "slug" : "screen-1"
  }
]

Source: https://weld-development-tom.firebaseio.com/projects/5649ddc3f709a10c003cf031/screens.json?print=pretty

How can I get the real screens collection through the JSON API?

1 Answer 1

2

This is how the Firebase database handles arrays.

In your situation, if you want an object rather than an array you'll need to change your keys from indexed based like an array. Unless the 1,2,3 ordering is important I would recommend using the .push() id generated from the Firebase SDK, or by doing a POST in over the REST API.

Check out the documentation on Arrays in Firebase data, which contains the code snippet below.

// we send this
['a', 'b', 'c', 'd', 'e']
// Firebase stores this
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}

// 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'}
Sign up to request clarification or add additional context in comments.

4 Comments

I understand that it becomes an array, but where does the null object come from? It's not present in my collection.
Probably because Arrays are 0-based, and null fills in for the 0 element.
^- this. Your index first index is 1, so since Array's must start with 0, Firebase backfills the first element in the array with null.
Well this is not that simple. Just experienced that if you delete one element from the middle, e.g. 'c', then you probably end up with array with 'null', like ['a', 'b', null, 'd', 'e'] . When the array is nullified and when objectified should be better defined by firebase, too much magic is never good.

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.