Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase Realtime Database equal_to() method gets the whole document instead of the parent only

I have this structure:

-User:
     -user1:
              -name : cycy
              -email: cycy@test.com

What I intend to do is know what parent does the input email contains. for example if my input email is cycy@test.com, I should be able to output "user1". Here is my code:

hey = db.child("User").order_by_child("email").equal_to("cycy@test.com").get()
print(hey.val())

My problem with this code is that it outputs everything and not the parent "user1" only
OrderedDict([('user1', {'name: cycy, email: cycy@test.com})])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can i modify this so that it only gives the parent "user1"

Thank you!

>Solution :

Firebase Realtime Database queries always give a list of results, as there may be multiple child nodes that match the query. Even when there’s only one result, you get a list of that one result. There is no way to change that behavior.

You will have to loop over the results, like shown in the Pyrebase documentation on handling a list of results. Based on that, your code would look something like:

for user in hey.each():
    print(user.key())
    print(user.val())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading