为什么Firebase集合似乎以空行开头? [英] Why do firebase collections seem to begin with a null row?
问题描述
我创建了一个简单的firebase数据集来测试一些REST调用(请参见下图).我想知道为什么,当我查询数据库中的集合时,firebase总是先返回空行,然后才返回实际行.这是导出数据时得到的信息.注意systems和system_types下的空行:
I've created a simple firebase data set to test some REST calls with (see the image below). I'm wondering why, when I query the collections in the database, firebase always return a null row first and then the actual rows. Here is what I get when I export the data. Notice the null rows under systems and system_types:
{
"systems" : [ null, {
"system_type_id" : 2,
"name" : "Commodore 128",
"id" : 1
}, {
"system_type_id" : 1,
"name" : "Difference Engine",
"id" : 2
}, {
"system_type_id" : 2,
"name" : "Osborne",
"id" : 3
} ],
"system_types" : [ null, {
"name" : "Babbage",
"id" : 1
}, {
"name" : "Von Neumann",
"id" : 2
} ]
}
推荐答案
Firebase会自动检测对象何时看起来像"数组,并相应地对其进行转换.由于'systems'和'system_types'的子级分别为1、2和3,因此Firebase会在输出时自动将其转换为数组.但是,由于您没有为索引0指定值,因此Firebase只是插入一个空元素.
Firebase automatically detects when an object 'looks like' an array, and converts it accordingly. Since 'systems' and 'system_types' have children of 1, 2, and 3, Firebase is automatically converting these to arrays at output time. However, since you didn't specify a value for index 0, Firebase is just inserting an empty element.
我建议您使用从零开始的ID,或者以非数字开头您的ID,因此我们知道它不是数组(例如:item1,item2,item3).
I'd suggest either using zero-based ID's, or perhaps starting your id's with something non-numeric so we know it's not an array (ie: item1, item2, item3).
您也可以忽略null元素-在Firebase中,null和不存在是同一件事.
You could also just ignore the null element -- In Firebase null and non-existent are the same thing.
这篇关于为什么Firebase集合似乎以空行开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!