Jose G
Jose G

Reputation: 75

How to retrieve the User info?

I want to retrieve data uid in User table data but for a specific user , I have 2 users, and it seems that it grabs the 2 users uid but I want to grab with i speficy not both of them just one. Thank You In advance

    let specificDatabase = Database.database().reference()

    specificDatabase.queryOrdered(byChild: "User/FirstName").queryEqual(toValue: "The user first name")

    specificDatabase.observeSingleEvent(of: .value) { (snapShot: DataSnapshot) in

        for child in snapShot.children {
            print(snapShot.key)
        }
    }

 Firebase Data Structure
"User" : {

"ez8sTAsqXTWfnuzizUXU69VS4qM2" : {
  "FirstName" : "other",
  "LastName" : "Martin",
  "uid" : "ez8sTAsqXTWfnuzizUXU69VS4qM2"
},
} 

 "Data" : {
"ez8sTAsqXTWfnuzizUXU69VS4qM2" : {
  "-Ll7jUYg6BxRAhWPLskg" : {
    "Name" : "other Martin",
    "Data1" : "data"
  },
  "-Ll7jW_elQIPTLESwDYD" : {
    "Name" : "other Martin",
    "Data1" : "data "
  }
},
 }

Upvotes: 0

Views: 135

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

You're telling Firebase to order each child node of the root by its User/FirstName property and then filter on that. Since the child nodes of the root don't have a property at that path, the query returns no results.

Instead you want to order/filter each child node of /User by itsFirstName property, which you can do with:

let specificDatabase = Database.database().reference(withPath: "User")

specificDatabase.queryOrdered(byChild: "FirstName").queryEqual(toValue: "other")

Upvotes: 1

Related Questions