0

I am trying to retrieve the documentIDs where the fields is the users email-adresse.

This is what have tried:

let emailID = Auth.auth().currentUser!.email!
let db = Firestore.firestore()

let vehicleArray = db.collection("Users").whereField("Email", arrayContains: emailID)
        .getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {

                    let vehicleArray2 = document.documentID

                    print(vehicleArray2)

                }

                    }

                        }

let print(vehicleArray)

The problem is that I get nothing then I print the vehicleArray, because the data from firestore are a string. The question is how can I get it in an array?

My Firebase Database looks like this:

Firebase Database

Thank you very much in advance for your help.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Data is loaded from Firestore asynchronously. Instead of waiting for the data to load, your main code (`let print(vehicleArray)`) continues to execute while the data is being loaded. Then once the data is available your closure is called and runs `print(vehicleArray2)`. So by the time the first print statement runs, the data isn't available yet. For this reason any code that needs the data must be in the closure, or be called from there. – Frank van Puffelen Jan 05 '20 at 16:18
  • Also see: https://stackoverflow.com/questions/51767310/asign-value-of-a-firestore-document-to-a-variable/51769180#51769180, https://stackoverflow.com/questions/58193483/result-of-call-to-functionname-is-unused/58194636#58194636, https://stackoverflow.com/questions/55195703/returning-name-from-firestore/55197677#55197677, https://stackoverflow.com/questions/59482689/storing-asynchronous-cloud-firestore-query-results-in-swift, and https://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift-function – Frank van Puffelen Jan 05 '20 at 16:21
  • Hi Frank, thank you for your input, but I seem not to understand it completely. Can you clarify it more? – Lasse Zinck Jan 06 '20 at 19:57
  • There is a ton of information in the questions/answers I linked. What *did* you take away from reading them and applying what you learned to your situation? And which part didn't you understand completely? – Frank van Puffelen Jan 06 '20 at 20:46

0 Answers0