Get firebase child values in python

Viewed 586

I am trying to get the string values for the children of each key in my database:

database preview

Here is the code for the function I have so far:

import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

def generator():
    ref = db.reference('users')
    users = ref.order_by_key().get() # order users in database

    for user, val in users.items():  # iterate until number of people in database is reached
        print(f'\n{val}')  # Print the users values
        who = ref.order_by_child('Who').get() # I need to store the users values into string variables
        what = ref.order_by_child('What').get()
        where = ref.order_by_child('Where').get()
        print(who)
        print(what)
        print(where)

Essentially, I am trying to iterate through every user in my realtime database and each time store their values for Who, What and Where into temporary variables which change for each user (I excluded the rest of the code because it is irrelevant to what I need to do).

As you can see I am printing the output for each variable, but I am getting error messages and I can't find anything to do this in python in the documentation.

Any ideas?

1 Answers

This looks wrong:

who = ref.order_by_child('Who').get()

The users snapshot that you get already contains the data for all users in the database, so you shouldn't have to do another get() call for each property of each user.

In your case the val is going to be a dictionary with the properties for a specific user. To get the value of a specific property, you can do:

who = val.get("Who")
Related