Putte
Putte

Reputation: 328

Error adding Firestore documents to Pickerview

I've recently changed a lot on my iOS application and now I got stuck.

I'm trying to insert data from Firestore which looks like this:

firebase

So, as you can see I've 6 different names in here.

And here is the code to insert into pickerView.

 func getPerson()
{
    let authentication = Auth.auth().currentUser?.uid
    db.collection("users").document(authentication!).collection("person").getDocuments { (QuerySnapshot, err) in

        //If error is not equal to nil
        if err != nil
        {
            print("Error getting documents: \(String(describing: err))");
        }
            //Succeded
        else
        {
            //For-loop
            for _ in QuerySnapshot!.documents
            {
                //Cleaning the array for the new values
                self.personArray.removeAll()

                let document = QuerySnapshot!.documents
                let data = document.data() //HERE IS THE ERROR

                data.forEach { (item) in
                    if let person1Data = data["name"] as? String
                    {
                        self.personArray.append(person1Data)
                        print(self.personArray)
                    }
                }
            }
            self.pickerView.reloadAllComponents()

        }
    }
}

I'm getting the error:

Value of type '[QueryDocumentSnapshot]' has no member 'data'

It used to have QuerySnapshot!.documents.first but it does not work anymore when I've changed the Firestore data.


Edit:

So. the output is now:

["Joche"] ["Joche", "Joche"] ["Putte"] ["Putte", "Putte"] ["Rebecca"] ["Rebecca", "Rebecca"] ["Fredrik"] ["Fredrik", "Fredrik"] ["Anna"] ["Anna", "Anna"] ["Vickan"] ["Vickan", "Vickan"]

which means it adds everything but x3. How to solve this problem?

Upvotes: 0

Views: 57

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

data is an instance method of a single QueryDocumentSnapshot not an array , You need

self.personArray.removeAll()
for elem in querySnapshot!.documents {
    let data = elem.document.data()
    data.forEach {
        if let person1Data = $0["name"] as? String {
            self.personArray.append(person1Data)
            print(self.personArray)
        }
    }
}

Upvotes: 1

Related Questions