在 Firebase 休息响应中获取空值
Getting a null value in Firebase rest response
我正在尝试使用 firebase 作为后端来创建 restful 服务。我创建了一个示例应用程序,当我点击此 url [https://poctest1.firebaseio.com/rest.json ] 时,我收到以下响应。
[
null,
{
"value": "test1"
},
{
"value": "test 2"
}
]
为什么我在顶部得到一个空值,我没有插入任何内容。
帮我解决这个问题。谢谢
您看到的是 Firebase 的 SDK 如何反序列化类似数组的结构。 Firebase's documentation on arrays 解释:
when data is read using val()
or via the REST api, if the data looks like an array, Firebase will render it as an array. In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array.
由于您的数组在索引 0 处没有元素,因此 Firebase 会为此注入 null
。
一般来说,在使用 Firebase 时,您应该尽量不要使用数组,原因在我上面链接的文档中提到过。如果您发现 必须 使用数组,请确保始终在 Firebase 中更新整个数组。所以不要试图操纵数组中的特定项目。
在零位加一个值,则不会得到空值
我正在尝试使用 firebase 作为后端来创建 restful 服务。我创建了一个示例应用程序,当我点击此 url [https://poctest1.firebaseio.com/rest.json ] 时,我收到以下响应。
[
null,
{
"value": "test1"
},
{
"value": "test 2"
}
]
为什么我在顶部得到一个空值,我没有插入任何内容。
帮我解决这个问题。谢谢
您看到的是 Firebase 的 SDK 如何反序列化类似数组的结构。 Firebase's documentation on arrays 解释:
when data is read using
val()
or via the REST api, if the data looks like an array, Firebase will render it as an array. In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array.
由于您的数组在索引 0 处没有元素,因此 Firebase 会为此注入 null
。
一般来说,在使用 Firebase 时,您应该尽量不要使用数组,原因在我上面链接的文档中提到过。如果您发现 必须 使用数组,请确保始终在 Firebase 中更新整个数组。所以不要试图操纵数组中的特定项目。
在零位加一个值,则不会得到空值