Tom Söderlund
Tom Söderlund

Reputation: 4747

Firebase collection becomes an array with null in Firebase JSON API

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?

Upvotes: 0

Views: 1003

Answers (1)

David East
David East

Reputation: 32614

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'}

Upvotes: 2

Related Questions