hugger
hugger

Reputation: 488

Firebase friendships node how to display user data with only uid

When showing a users friend list with this db structure:

friendships:
 user1
    - user3: true
    - user4: true
 user2
    - user1: true 
    - user5: true 

What would be the best way to display the users data from their users node based off their uid (which is actually the key in my example rather than userX).

So for user1, his friend list should have user3 and user4 displaying in a list with their username and profilePic for example... I can’t wrap my head around a good solution rather than downloading the whole users node and finding a match for the uids to list out (which would result in a lot of unwanted $$$ downloads)

Cheers!

Upvotes: 0

Views: 25

Answers (1)

Neel Rao
Neel Rao

Reputation: 153

You need a separate table with user data.

users:
  user1:
    username: bob
    email: [email protected]
    etc.
  user2:
    username: jane
    email [email protected]

Then, you can query the users object by uid like this:

firebase.database().ref('/users').child('user1').once("value", snapshot => {
    let userData = snapshot.val();
    let username = userData.username;
}

And do that for each friend, therefore you are only downloading the data for each friends, rather than every user.

Upvotes: 1

Related Questions