从UID检索Firebase信息

时间:2020-03-07 13:24:48

标签: ios swift firebase google-cloud-firestore firebase-authentication

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

我正在尝试通过将当前UID与firebase文档中的用户名匹配,然后仅从中提取用户名来获取当前用户的登录名,当我使用此查询时,我得到“文档不存在”错误,我的查询哪里出问题了?

1 个答案:

答案 0 :(得分:1)

您没有得到任何结果,因为您的文档ID与您的用户ID不同。而是这样创建查询:

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

或者如果您希望通过查询文档ID来进行查询,请在创建文档时将文档ID设置为与uid相同。

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