使用Firebase控制台在Firebase中设置数组 [英] Setting arrays in Firebase using Firebase console
问题描述
解决方案
Firebase数据库不存储数组。它存储字典/关联数组。所以最接近你可以得到:
pre $ 参加者:{
0:Bill Gates,
1:Larry Page,
2:James Tamplin
}
您可以在Firebase控制台中构建此结构。然后,当您使用其中一个Firebase SDK读取它时,它将被转换为数组。
firebase.database()一次('价值',功能(快照){
console.log(snapshot.val());
// [比尔盖茨,拉里页 ,James Tamplin]
});
所以这可能是你寻找的结果。但我建议阅读这篇博客文章,了解为什么如果不存储阵列,Firebase更喜欢它: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html 。
不要使用一个数组,当你真的需要一个集合
大多数开发人员实际上并没有试图存储一个数组,我想你的情况可能是其中之一。例如:Bill Gates可以两次参加吗?
与会者:{
0:Bill Gates,
1:Larry Page,
2:James Tamplin,
3:Bill Gates
}
$ b $如果不是的话,你必须在添加他之前检查他是否已经在数组中。
如果(!attendees.contains(Bill Gates)){
attendees.push(Bill Gates);
$ b $ p
$ b 这是一个明显的迹象,表明你的数据结构对于使用而言是次优的-案件。在添加新的孩子之前,必须检查所有现有的孩子将会限制可扩展性。在这种情况下,您真正需要的是一个集合:一个数据结构,其中每个孩子只能出现一次。在Firebase中,您的模型设置如下:
参加者:{
Bill Gates:true,
Larry Page:true,
James Tamplin:true
}
现在,每当您尝试添加比尔盖茨第二次,这是一个无操作:
attendees [比尔盖茨 ] = true;
所以不必为唯一性需求编写代码,数据结构隐式地解决了这个问题。 >
I am using Firebase console for preparing data for a demo app. One of the data item is attendees. Attendees is an array. I want to add a few attendees as an array in Firebase. I understand Firebase does not have arrays, but object with keys (in chronological order). How do I do that for preparing sample data? My current Firebase data looks like the below.
解决方案 The Firebase Database doesn't store arrays. It stores dictionaries/associate arrays. So the closest you can get is:
attendees: {
0: "Bill Gates",
1: "Larry Page",
2: "James Tamplin"
}
You can build this structure in the Firebase Console. And then when you read it with one of the Firebase SDKs, it will be translated into an array.
firebase.database().ref('attendees').once('value', function(snapshot) {
console.log(snapshot.val());
// ["Bill Gates", "Larry Page", "James Tamplin"]
});
So this may be the result that you're look for. But I recommend reading this blog post on why Firebase prefers it if you don't store arrays: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html.
Don't use an array, when you actually need a set
Most developers are not actually trying to store an array and I think your case might be one of those. For example: can "Bill Gates" be an attendee twice?
attendees: {
0: "Bill Gates",
1: "Larry Page",
2: "James Tamplin",
3: "Bill Gates"
}
If not, you're going to have to check whether he's already in the array before you add him.
if (!attendees.contains("Bill Gates")) {
attendees.push("Bill Gates");
}
This is a clear sign that your data structure is sub-optimal for the use-case. Having to check all existing children before adding a new one is going to limit scalability.
In this case, what you really want is a set: a data structure where each child can be present only once. In Firebase you model sets like this:
attendees: {
"Bill Gates": true,
"Larry Page": true,
"James Tamplin": true
}
And now whenever you try to add Bill Gates a second time, it's a no-op:
attendees["Bill Gates"] = true;
So instead of having to code for the uniqueness requirement, the data structure implicitly solves it.
这篇关于使用Firebase控制台在Firebase中设置数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!