How do I go through this database and get child values?

I have this database that looks like this enter image description here

therefore, the first keys are the user uid taken from auth, and then the username that he provided, and that they scored for each match, are also accepted.

I just wanted to get the total number of users - for example, Rey the total number of points is 45 and Tree : 44 , but after viewing the documents, all I could do was just for one user, I have to write each username and a specific correspondence for each line, to get the value. Now think about how it will be if they are dozens of users? hmm a lot of lines ..

here is json

javascript code

var query = firebase.database().ref(); query.once("value") .then(function(snapshot) { snapshot.forEach(function(childSnapshot) { var key = childSnapshot.key; var Data1 = childSnapshot.child("Ray/Match1/Points").val(); var Data2 = childSnapshot.child("Ray/Match2/Points").val(); console.log(Data1 + Data2); }); }) 

which will allow me to display the total number of Ray points, but not for Wood, it is obvious that I have to repeat it and write it down.

So how can I solve this?

+7
javascript firebase firebase-database
source share
4 answers

I took a look at your problem, and I think I have your solution, or at least PATHWAY for your solution. Ok, first I will explain the main problem, then I will try to provide you with generic-ish code (I will try to use some of the variables you use). And off we go!

Basically what I see is 2 steps ...

STEP 1 - you need to use the constructor function, which will create new user objects with their name (and / or user ID) and their own set of properties.

From this point of view, you can have a constructor function that includes properties such as "username", "match points 1", "match points 2", and then a function in which the console registers a summary of each name and their common points from the match points 1 and 2.

STEP 2. You need to place the constructor function inside the loop, which will go through the database, looking for specific properties needed to populate the properties needed by the constructor function, to highlight the information you are looking for.

So ... and let's take a deep breath, because it was a lot of words ... let's try to do it. I will use common properties in such a way that, in my opinion, it will be easier for you to insert your own property / variable names.

 var user = function(name, match1, match2){ this.name = name; this.match1 = match1; this.match2 = match2; this.pointTotal = function(match1, match2) { console.log(match1 + match2);}; this.summary = function(){ console.log(name + " has a total of " + pointTotal + " points.");}; } 

part of the "This" code allows you to use any username, not just certain ones.

Ok, so the above code takes care of part of the constructor function of the problem. Now it doesn’t matter how many users you need to create with unique names.

The next step is to create some kind of loop function that will go through the database and fill in the properties needed to create each user so that you can get total points from the EVERY user, and not just one.

Again, I will use generic-ish property / variable names ...

 var key = childSnapshot.key; while(i = 0; i < key.length + 1; i++) { var user = function(name, match1, match2){ this.name = name; this.match1 = match1; this.match2 = match2; this.pointTotal = function(match1, match2) { console.log(match1 + match2);}; this.summary = function(){ console.log(name + " has a total of " + pointTotal + " points.");}; } } 

This is a lot of words, and the code is a hybrid of common names / property variables and property names / variables used by you, but I'm sure I'm on the right track.

I have great confidence that if you used the code and EXPLANATION that I provided, then if you connect your own variables, you will get the solution you need.

In conclusion, I just want to say that I really hope that this helps, and if it is not, I would like to help solve the problem anyway, because I need practice. I work with strange watches, so if I don’t answer right away, I’ll most likely be in my work :(

Good luck and I hope that helped!

+5
source share

just add total node to your db

 |_Id |_ $userId: | |_ Ray | | |_ Match1:24 | | |_ Match2:21 | |_ total:45 

and then get the total number of users

 var query = firebase.database().ref(); query.once("value") .then(function(snapshot) { snapshot.forEach(function(childSnapshot) { var total = childSnapshot.child("total").val(); console.log(total); }); }) 

you can add a generic node using cloud functions

+3
source share

Check out this implementation. No cloud feature required.

  firebase().database().ref().on('value', function(snapshot) { snapshot.forEach((user)=>{ user.forEach((matches)=> { var total = 0; matches.forEach((match)=> { total += match.val().Points; }); console.log(total); }); }); }) 
+2
source share

If the key is the user identifier, why add another nested object with the username? Do you expect one user to have multiple usernames? This sounds strange and adds complexity, as you probably noticed. If you need to save the username somewhere in Firebase, it is recommended that you highlight the user information section somewhere directly under the user key. Here is a JavaScript representation of the structure of a Firebase object:

 { a1230scfkls1240: { userinfo: { username: 'Joe' }, matches: { asflk12405: { points: 123 }, isdf534853: { points: 345 } } } } 

Now, getting into common points seems a little easier, doesn't it? 😎

To help you without changing the current database structure, you just need to iterate over all the user names userId + username + matches in your database. Here is a sample code to achieve just that, you don’t need a special Firebase function, just the old old JavaScript for the loop:

 const query = firebase.database().ref(); query.once('value') .then(snapshot => { const points = {} const users = snapshot.val() for (const userId of Object.keys(users)) { const userprofile = users[userId] for (const username of Object.keys(userprofile)) { const user = userprofile[username] for (const matchId of Object.keys(user)) { const match = user[matchId] // Store the points per user, per profile, or per both, depending on your needs points[username] = points[username] === undefined ? points[username] = match.points : points[username] += match.points } } } }) 
+1
source share

All Articles