Dan Levy
Dan Levy

Reputation: 4271

Firebase Query Database returns null for username

I am trying to query this database:

enter image description here

I am using this line of code:

databaseReference.child("users").queryOrdered(byChild: "username").queryEqual(toValue: "billsmith").observeSingleEvent(of: .value, with: { (snap) in
    print(snap)
})

This line of code returns null - meaning it did not find "jondoe" as a username in the users node. How do I get this to work?

Upvotes: 0

Views: 66

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Two problems:

  1. the type usernames vs username
  2. you're trying to query a deeper property, but only present the property name instead of its path

This should work better:

databaseReference
  .queryOrdered(byChild: "userDetails/username")
  .queryEqual(toValue: "billsmith")
  .observeSingleEvent(of: .value, with: { (snap) in
    print(snap)
  })

Upvotes: 1

Related Questions