draw134
draw134

Reputation: 1187

Firebase get data based on key from parent

I got a parent table which is users and it has 3 childs in it. Can I access a child's data by just passing an uid? Here is my user structure

enter image description here

What I tried is

    firebase.database().ref().child('users').equalTo(localStorage.getItem('uid')).once('value',snap =>{
      console.log(snap.val())
    })

I get an null value everytime. I just want to grab what type of user when logging in so that I can display the appropriate UI for every user. Im just new to firebase.

Upvotes: 0

Views: 105

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317392

Your database structure does not allow the kind of query you are trying to perform. You can only filter child nodes at one level deep from root node of your query. The root node here is "users", but your actual user data is two levels deep under it.

If you want to find the type of user given only a UID, you will need a structure that puts all users under the same root node:

- users
   - {uid}
       - type: "doctors" | "patients" | "secretary"
       - email: ...
       - name: ...

Now you can very easily query this for a UID to find out what type it is

firebase.database().ref().child('users').child(uid).once('value', ...)

Upvotes: 1

Related Questions