Firebase database child reference

Ragul Cs :

enter image description here

Those rounds refers to the user ID(uid), My question is how can I refer to a child of the current user I tried a lot like forEach, orderByKey etc.., but I could'nt find a solution so anyone can help me? (Note: I'm a beginner)

Peter Haddad :

Change the structure of the database to the following:

Accounts
   userId
      randomId
          age : 100
          name : peter
   userId
      randomId
          age : 120
          name : peterx

This way it will be easier to query, now you can do the following:

let ref = firebase.database().ref("Accounts");
let user = firebase.auth().currentUser;

ref.child(user.uid).on("value",(snapshot)=> {
 snapshot.forEach((childSnapshot) => {
  let childData = childSnapshot.val();
  let age = childData.age;
  let name = childData.name;
  console.log(childData);
 });
});

Another way is to remove the randomId and directly access the data without using forEach.

https://firebase.google.com/docs/auth/web/manage-users

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31788&siteId=1