I'm currently creating an entry in the Firestore as following:
db.collection(userId).document(title).collection(subTitle).addDocument(data: [
"key": "value"
])
I'm fetching the data as following. I understand that Firebase only allows shallow queries and I'm only looking to get the list of immediate documents (and not the sub collections):
self?.db.collection(user!.uid)
.addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
print("Error fetching document: \(error!)")
return
}
print("document", document)
document.documents.forEach { (snapshot) in
print("snapshot", snapshot)
let data = snapshot.data()
print("data", data)
}
}
But, I'm only getting <FIRQuerySnapshot: 0x7fba4cd2baa0>
from document
and there's nothing to be parsed.
I've also tried:
self?.db.collection(user!.uid).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
I get the same result as above.
However, when I create an entry as following (without the sub collection), I can retrieve the data perfectly fine:
self.db.collection(userId).document(title).setData([
"key": "value"
])
The hierarchy
- First collection
- First document
- Second collection
- Second document (with randomly generated ID from "addDocument")
- Fields
What's frustrating is that even if I reference a specific document by its name, the query returns empty:
let docRef = self?.db.collection(user!.uid).document("0x6cac0720c8537467d7d2dccd9b88b8c757e10dfd3dcb8991ff71a4f8dfe1dd")
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")
}
}