Firebase集合在Firebase JSON API中变为null数组

时间:2016-02-03 09:11:50

标签: firebase

我有一个Firebase集合,如下所示:

screens collection

来源:https://weld-development-tom.firebaseio.com/projects/5649ddc3f709a10c003cf031/screens(需要身份验证)

但是当我通过Firebase JSON API请求它时,它看起来像这样:

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

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

如何通过JSON API获取真正的screens集合?

1 个答案:

答案 0 :(得分:1)

这是Firebase数据库处理数组的方式。

在您的情况下,如果您想要一个对象而不是一个数组,您需要将您的键从基于索引的键更改为数组。除非1,2,3排序很重要,否则我建议您使用Firebase SDK or by doing a POST in over the REST API生成的.push() ID。

Check out the documentation on Arrays in Firebase data,其中包含以下代码段。

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