Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  68] [ 2]  / answers: 1 / hits: 25643  / 7 Years ago, thu, march 16, 2017, 12:00:00

I am trying to read the data from firebase database, and display the same in a webpage.



My database structure is as below -
Image



If you see the image, i am able to read the UserData using the below code -



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



Now, I need to fetch the values for One, Count by matching the key values with available AssignedID.



How can i achieve the same?



Update, JSON as text -



{
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
}
}
}
}

More From » jquery

 Answers
14

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 teamsSnapshot to get each teamSnapshot and then loops over that to get each team property.



Working jsbin: http://jsbin.com/noziri/edit?html,js,console


[#58528] Tuesday, March 14, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tina

Total Points: 91
Total Questions: 106
Total Answers: 104

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
tina questions
;