Retrieving firebase info based from a UID

Dread :
// if im ride leader, fetch details from firebase using my UID for name ect.
@IBAction func RideLeaderSwitchOn(_ sender: Any) {
   // hide feild for other user search
    if RideLeaderSwitch.isOn{
        OtherRideLeaderFeild.isHidden = true
        // perform databse search for current user info
        let user = Auth.auth().currentUser
        let db = Firestore.firestore()
        if let user = user{
            let uid = user.uid
            let docRef = db.collection("users").document(uid)
                docRef.getDocument { (document, error) in
                   if let document = document, document.exists {
                       let property = document.get("firstName")
                    print("Document data: \(String(describing: property))")
                   } else {
                       print("Document does not exist")
                   }
               }
            }
        }else{
         // if im NOT ride lead, Get user to enter another users First and Last Name and do an firebase search for a matching user
        OtherRideLeaderFeild.isHidden = false
    }
}

Database layout

I am trying to get the first name of the current user signed in by matching the current UID to the one in firebase document and then pulling the first name only from it, when I use this query I get my "document does not exist" error, where am I going wrong with my query?

Vasil Garov :

You don't get any results because your document id is not the same as your user id. Instead create the query like this:

let docRef = db.collection("users")
               .whereField("uid", isEqualTo: uid)
               .getDocuments { (snapshot, error) in
    print(snapshot.documents)
}

Or if you want your query to work by querying the document id, set the document id to be the same as your uid when you create the document.

db.collection("users").document(uid).setData(["item": "test"]) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33265&siteId=1