Firebase 强制使用地图而不是数组
Firebase force to use map rather than array
我在 firebase 指南中读到了数组:
https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-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 只使用 map 而从不将数据转换为数组?有什么配置吗?
在我的 firebase 示例中,我有这样的结构:
{ "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 很智能并且可以将我的数据转换为数组但是,我希望映射。这种行为对我来说非常
不稳定。
[ {
"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
}
}
} ]
从不获取数组的最简单方法是始终使用字符串作为键。如果您的自然键是数字,请在它们前面加上字符串。
例如
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 永远不会将其强制转换为数组。
我在 firebase 指南中读到了数组: https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-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 只使用 map 而从不将数据转换为数组?有什么配置吗?
在我的 firebase 示例中,我有这样的结构:
{ "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 很智能并且可以将我的数据转换为数组但是,我希望映射。这种行为对我来说非常 不稳定。
[ {
"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
}
}
} ]
从不获取数组的最简单方法是始终使用字符串作为键。如果您的自然键是数字,请在它们前面加上字符串。
例如
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 永远不会将其强制转换为数组。