【问题标题】:Retrieving firebase info based from a UID从 UID 检索 firebase 信息
【发布时间】:2020-03-07 13:24:48
【问题描述】:
// 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
    }
}

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

【问题讨论】:

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


    【解决方案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"]) 
    

    【讨论】:

    • 非常感谢!我完全错过了文档 ID 不等于 UID。
    • @Dread 虽然这是一个很好的答案。您确实应该使用 documentId 作为用户 UID 来存储用户。查询非常“繁重”,比直接读取数据要慢。如果 documentId 是用户 UID,您可以完全避免查询,因为您可以直接读取节点。它还将消除在文档中存储 UID,从而节省读取这些字节的时间。
    • 对我来说,firebase.firestore().collection("users").document 是未定义的...
    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 2021-02-03
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2021-09-09
    相关资源
    最近更新 更多