简体   繁体   English

从 UID 检索 Firebase 信息

[英]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?我试图通过将当前 UID 与 firebase 文档中的 UID 匹配来获取当前用户的名字登录,然后仅从中提取名字,当我使用此查询时,我收到“文档不存在”错误,我的查询哪里出错了?

You don't get any results because your document id is not the same as your user id.您没有得到任何结果,因为您的文档 ID 与您的用户 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.或者,如果您希望通过查询文档 ID 来进行查询,请在创建文档时将文档 ID 设置为与您的 uid 相同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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