Sai Kumar
Sai Kumar

Reputation: 67

Not able to get the sub collection data

I have on collection name > User doc id > sub collection > auto id > some data.

I need to retrieve my "some data". I tried my below code. But i am not getting the exact data available in my each auto generated id

var location: [DocumentSnapshot] = []

This is the method i used :

Firestore.firestore().collection("Users").document("KWfMNamZ4dN0DfQppLl33Ng692F3").collection("Relations").getDocuments { (querySnapshot: QuerySnapshot?, error: Error?) in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    self.location = (querySnapshot?.documents)!
                    print(querySnapshot)
                    //[<FIRQueryDocumentSnapshot: 0x2803db100>, <FIRQueryDocumentSnapshot: 0x2803db1c0>]
                    print(self.location)
                }
            }

But when i print my query i am getting always :

[<FIRQueryDocumentSnapshot: 0x2803db100>, <FIRQueryDocumentSnapshot: 0x2803db1c0>]

I have one collection name called "Users". It have some "Uid". In that particular Uid i have one more sub collection called "Relations". This relations have an auto gen id with some data.

So totally i may have one or many auto gen id , each id have some data.So i needs to fetch that all data , not only auto gen id.

How can i achieve that.Any solution would be helpful

Thanks

Upvotes: 1

Views: 210

Answers (1)

spike04
spike04

Reputation: 126

Get your document data like below:

for document in querySnapshot!.documents {

    print("\(document.documentID) => \(document.data())")

 }

It will give you the id with the data.Refer this Firestore

Upvotes: 1

Related Questions