Firebase - 通过键或 chid 值获取数据 - javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42824688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase - Get data by key or chid values - javascript
提问by Prashanth kumar
I am trying to read the data from firebase database, and display the same in a webpage.
我正在尝试从 firebase 数据库读取数据,并在网页中显示相同的数据。
My database structure is as below -

我的数据库结构如下 -

If you see the image, i am able to read the "UserData" using the below code -
如果您看到图像,我可以使用以下代码读取“ UserData” -
firebase.initializeApp(config);
var database = firebase.database();
var ref = database.ref('UserData');
ref.once('value', gotData1, errData);
function gotData1(data){
//console.log(data.val());
var usrData = data.val();
var keys = Object.keys(usrData);
//console.log(keys);
for (var i = 0; i< keys.length; i++){
var k = keys[i];
var id = usrData[k].AssignedID;
var name = usrData[k].Name;
$(document).ready(function() {
var $formrow = '<tr><td>'+id+'</td><td>'+name+'</td></tr>';
$('#userInfo').append($formrow);
});
}
}
In the highlighted part of the image, you can see keys with values 196214, 196215, 196216
在图像的突出显示部分,您可以看到值为 196214、196215、196216 的键
Now, I need to fetch the values for "One, Count"by matching the key values with available AssignedID.
现在,我需要通过将键值与可用的 AssignedID 进行匹配来获取“One, Count”的值。
How can i achieve the same?
我怎样才能达到同样的目标?
Update, JSON as text -
更新,JSON 作为文本 -
{
"app_url" : "https://app_name?ls=1&mt=8",
"UserData" : {
"HNpTPoCiAYMZEeVOs01ncfGBj6X2" : {
"Name" : "Arunima Vj"
"Email" : "[email protected]",
"AssignedID" : 196214
},
"VXU2tdGdzZX90PJa9mpEL3zAiZo2" : {
"Name" : "Lakshman Medicherla"
"Email" : "[email protected]",
"AssignedID" : 196215
},
"dFlwtqDNrja2RkOySVtW106IQP62" : {
"Name" : "Prashanth Sripathi"
"Email" : "[email protected]",
"AssignedID" : 196216
}
}
"teams" : {
"196214" : {
"1105" : {
"One" : 7619,
"count" : 24
},
"1379" : {
"Two" : 7145,
"count" : 21
}
},
"196215" : {
"1111" : {
"One" : 7779,
"count" : 20
},
"1508" : {
"Two" : 1176,
"count" : 21
}
},
"196216" : {
"1106" : {
"One" : 7845,
"count" : 22
},
"1509" : {
"Two" : 1156,
"count" : 26
}
}
}
}
采纳答案by Frank van Puffelen
Your data structure is quite nested, which makes the code more difficult to read. But this navigates the structure generically in the minimum code I could come up with:
您的数据结构非常嵌套,这使得代码更难阅读。但这在我能想出的最少代码中一般地导航结构:
var ref = firebase.database().ref("/42824688");
ref.child("UserData").once('value', gotUserData);
function gotUserData(snapshot){
snapshot.forEach(userSnapshot => {
var k = userSnapshot.key;
var id = userSnapshot.val().AssignedID;
var name = userSnapshot.val().Name;
ref.child("teams").child(id).once("value", teamsSnapshot => {
teamsSnapshot.forEach(teamSnapshot => {
var teamKey = teamSnapshot.key;
teamSnapshot.forEach(teamProp => {
var prop = teamProp.key;
var val = teamProp.val();
console.log(k+" "+name+" "+id+": "+teamKey+", "+prop+"="+val);
});
});
});
})
}
So for each user, this loads the teams data for that user and then loops over the teamsSnapshotto get each teamSnapshotand then loops over thatto get each team property.
因此,对于每个用户,此负载该用户的数据团队,然后循环通过teamsSnapshot让每一个teamSnapshot,然后遍历该让每个团队属性。
Working jsbin: http://jsbin.com/noziri/edit?html,js,console
工作 jsbin:http://jsbin.com/noziri/edit?html,js,console

