简体   繁体   中英

Retrieving firebase info based from a UID

// 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
    }
}

数据库布局

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?

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"]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM