Firebase 强制使用地图而不是数组 [英] Firebase force to use map rather than array

查看:23
本文介绍了Firebase 强制使用地图而不是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 firebase quide 中,我读到了有关数组的信息:https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase

In firebase quide i read about array: https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase

特别是:

但是,为了帮助在 Firebase 数据库中存储数组的开发人员,当使用 val() 或通过 REST api 读取数据时,如果数据看起来像一个数组,Firebase 客户端会将其呈现为一个数组.特别是,如果所有键都是整数,并且对象中 0 和最大键之间的键有一半以上具有非空值,则 Firebase 客户端会将其呈现为数组.后一部分很重要,要记住.

However, to help developers that are storing arrays in a Firebase database, when data is read using val() or via the REST api, if the data looks like an array, Firebase clients 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 clients will render it as an array. This latter part is important to keep in mind.

有什么方法可以强制 firebase 只使用地图而不将数据转换为数组?有什么配置吗?

Is there any way to force firebase to use only map and never convert data to array? Any configuration?

在我的 firebase 示例中,我有这样的结构:

In my example in firebase i have got structure like that:

{ "0": {
  "drivers" : {
    "12" : {
      "latitude" : 50.076574,
      "longitude" : 19.9209708,
      "timestamp" : 1456311442329
    },
    "13" : {
      "latitude" : 50.0166148,
      "longitude" : 20.9863258,
      "timestamp" : 1456395866163
    }
  }
}
},
{ "1" : {
  "drivers" : {
    "10" : {
      "driver_id" : 10,
      "latitude" : 50.076574,
      "longitude" : 19.9209708,
      "timestamp" : 1456311442329
    },
    "17" : {
      "driver_id" : 17,
      "latitude" : 50.0166148,
      "longitude" : 20.9863258,
      "timestamp" : 1456395866163
    }
  }
} 
}

在java脚本中,我像数组一样读取json,因为firebase很聪明,可以将我的数据转换为数组,但是,我希望map.这种行为对我来说非常不稳定.

And in java script i read json like array, because firebase is smart and convert my data to array but, i expect map. This behavior for me is very unstable.

 [ {
      "drivers" : {
        "12" : {
          "latitude" : 50.076574,
          "longitude" : 19.9209708,
          "timestamp" : 1456311442329
        },
        "13" : {
          "latitude" : 50.0166148,
          "longitude" : 20.9863258,
          "timestamp" : 1456395866163
        }
      }
    }, {
      "drivers" : {
        "10" : {
          "driver_id" : 10,
          "latitude" : 50.076574,
          "longitude" : 19.9209708,
          "timestamp" : 1456311442329
        },
        "17" : {
          "driver_id" : 17,
          "latitude" : 50.0166148,
          "longitude" : 20.9863258,
          "timestamp" : 1456395866163
        }
      }
    } ]

推荐答案

从不获取数组的最简单方法是始终使用字符串作为键.如果您的自然键是数字,请在它们前面加上一个字符串.

The easiest way to never get an array is to always use strings as keys. If your natural keys are numbers, prefix them with a string.

例如

var words = ['zero', 'one', 'two', 'three'];
words.forEach(function(word, index) {
  ref.child('word_'+index).set(word);
});

这样你就会得到:

{
  "word_0": "zero",
  "word_1": "one",
  "word_2": "two",
  "word_3": "three"
}

Firebase 永远不会将其强制转换为数组.

And Firebase will never coerce it into an array.

这篇关于Firebase 强制使用地图而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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