5

I'm trying to get the firstname from this: enter image description here

The user is logged in and I can access his uid, but I can't get the Document that belongs to that user, this is how I'm trying to do it:

    private func getDocument() {

        var userID = Auth.auth().currentUser?.uid
            userID = String(userID!)

        var currentUser = Auth.auth().currentUser


//         Get sspecific document from current user
        let docRef = db.collection("users").document("bnf2s3RURUSV2Oecng9t")

        // Get data
        docRef.getDocument { (document, error) in
            if let document = document, document.exists {
                let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
                print("Document data: \(dataDescription)")
            } else {
                print("Document does not exist")
            }
        }
    }

If you noticed, if I put manually "bnf2s3RURUSV2Oecng9t" it will access the data, but the point is to be able to know what ""bnf2s3RURUSV2Oecng9t" is so I can put it on that hard coded variable on top. My end goal is be able to get the first name of the current user :)

1 Answer 1

9

The problem is that your current authenticated user uid is not the same as the document uid. So normally if those ids where the same you would do something like this:

    private func getDocument() {
        //Get specific document from current user
        let docRef = Firestore.firestore()
            .collection("users")
            .document(Auth.auth().currentUser?.uid ?? "")

        // Get data
        docRef.getDocument { (document, error) in
            guard let document = document, document.exists else {
                print("Document does not exist")
                return
            }
            let dataDescription = document.data()
            print(dataDescription?["firstname"] ?? "")
        }
    }

But in your case you would need to do it like this, since those values are not the same.

    private func getDocument() {
         //Get specific document from current user
         let docRef = Firestore.firestore()
            .collection("users")
            .whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "")

         // Get data
         docRef.getDocuments { (querySnapshot, err) in
             if let err = err {
                 print(err.localizedDescription)
             } else if querySnapshot!.documents.count != 1 {
                 print("More than one document or none")
             } else {
                 let document = querySnapshot!.documents.first
                 let dataDescription = document?.data()
                 guard let firstname = dataDescription?["firstname"] else { return }
                 print(firstname)
             }
         }
     }
8
  • When I type print(docRef) it says: <FIRDocumentReference: 0x600001605fe0> instead of bnf2s3RURUSV2Oecng9t also the result says: Document does not exist
    – Arturo
    Nov 6, 2019 at 21:28
  • well db was already let db = Firestore.firestore() . I tried your new code and I get the same
    – Arturo
    Nov 6, 2019 at 21:34
  • print(Auth.auth().currentUser?.uid ?? "nil") is going to print this: XIbRKL7cK9N6Y4N7lRaoBsw2R5A3
    – Arturo
    Nov 6, 2019 at 21:35
  • isn't the uid the one from the user? Which is printing it correct: XIbRKL7cK9N6Y4N7lRaoBsw2R5A3
    – Arturo
    Nov 6, 2019 at 21:44
  • 1
    Excellent! Thank you very much for all your help :)
    – Arturo
    Nov 6, 2019 at 22:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.