如何引用存储在Firestore中的uid?

时间:2019-08-16 16:08:13

标签: ios swift google-cloud-firestore

我正在尝试添加一个引用当前用户uid的常量,但是它访问红色的常量而不是蓝色的常量(请参见图片)

这是我到目前为止所写的内容:

  let user = Auth.auth().currentUser!
            let db = Firestore.firestore()
            let document = db.collection("users").document(user.uid);
            document.getDocument(completion: {(snapshot, err) in
                if let name = snapshot?.data()?["first name"] as? String
                {
                    self.userNameLabel.text = name
                }
                else
                {   print(user.uid)
                    self.userNameLabel.text = "Didn't work"
                }
            })

    }

1 个答案:

答案 0 :(得分:1)

尝试一下

    let db = Firestore.firestore()

    db.collection("users").whereField("uid", isEqualTo: user.uid)
        .getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    print(document.data()["first_name"])
                }
            }
    }