Firebase中的单个整数(Firebase数组行为) [英] Single integer as key in firebase (Firebase array behavior)

查看:145
本文介绍了Firebase中的单个整数(Firebase数组行为)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将数据插入到firebase中的 node / a / 0 中,那么结果会处理 a 是一个数组 a [0]



如果我将数据设置为 node / a / 1 ,那么第一个数组将变为null。

 a:[null,{
-J-22j_Mb59l0Ws0H9xc:{
name:chok wee ching
}
}]

但如果 node / a / 2

 a:{
2:{
-J-25xjEXUqcpsC -5LOE:{
name:chok wee ching
}
}
}



我的猜测,firebase自动将单个0和1视为一个数组。
如何防止这种情况发生?

解决方案

Firebase没有对数组的原生支持。如果你存储一个数组,它实际上被存储为一个整数作为键名的对象。

然而,为了帮助那些在Firebase中存储数组的人,你调用.val()或使用REST API从Firebase读取数据,如果数据看起来像一个数组,那么Firebase将把它呈现为一个数组。



特别是,如果所有的键都是整数,并且在对象中的0和最大键之间的键的一半以上都有非空值,那么Firebase将把它呈现为一个数组。这是你遇到的启发式的后期部分。你的第二个例子只设置了2的值,而不是0和1,所以只有不到一半的值有值,因此Firebase将它作为一个对象呈现。



你目前不能改变或阻止这种行为(尽管这是一个常见的混乱来源,所以我们可能会在未来做一些调整)。但是一旦你了解它,处理这个行为通常是很容易的。如果你需要进一步的帮助,也许你可以扩大你的问题来解释你需要完成什么,以及这种行为如何阻止它。


If I insert data into node/a/0 in firebase.

the result will treat a is an array a[0].

The same way, if I set my data in node/a/1 the first array will become null

      "a" : [ null, {
        "-J-22j_Mb59l0Ws0H9xc" : {
          "name" : "chok wee ching"
        }
      } ]

But it will be fine if node/a/2

      "a" : {
        "2" : {
          "-J-25xjEXUqcpsC-5LOE" : {
            "name" : "chok wee ching"
          }
        }
      }

my guess, firebase automatically treat single 0 and 1 as an array. How can I prevent this?

解决方案

Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.

However, to help people that are storing arrays in Firebase, when you call .val() or use the REST api to read data from Firebase, 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. It's this latter part of the heuristic that you are running into. Your second example only sets a value for 2, not 0 and 1, so less than half of the keys have values, and therefore Firebase renders it as an object.

You can't currently change or prevent this behavior (though it's a common source of confusion so we'll likely make some tweaks here in the future). However it's usually very easy to deal with the behavior once you understand it. If you need further help, perhaps you can expand your question to explain what you need to accomplish and how this behavior is preventing it.

这篇关于Firebase中的单个整数(Firebase数组行为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆