High level: In Cloud Firestore, I have two collections. fl_content and fl_files. Within fl_content, I am trying to access fl_files.
Detailed: In fl_content, each document has a field called imageUpload. This is an array of Firebase Document References. (a path to fl_files that I need to access.)
Here's my query for fl_content, in which I am accessing imageUpload reference:
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload")
print("PROPERTY \(property!)")
}
}
This prints the following to the console:
PROPERTY Optional(<__NSArrayM 0x60000281d530>(
<FIRDocumentReference: 0x600002826220>
)
)
With this array of Document References, I need to get to fl_files.
This is the part I am having trouble with.
Attempts:
Within the if let statement, I tried accessing fl_files by casting property as a DocumentReference.
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload") as? DocumentReference
print("PROPERTY \(property!)")
let test = Firestore.firestore().collection("fl_files").document(property)
}
}
Cannot convert value of type 'DocumentReference?' to expected argument type 'String'
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload") as! DocumentReference
let test = Firestore.firestore().collection("fl_files").document(property[0].documentID)
print("TEST \(test)")
}
}
Value of type 'DocumentReference' has no subscripts
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload") as! DocumentReference
let test = Firestore.firestore().collection("fl_files").document(property.documentID)
print("TEST \(test)")
}
}
Could not cast value of type '__NSArrayM' (0x7fff87c50980) to 'FIRDocumentReference' (0x10f6d87a8). 2020-02-05 12:55:09.225374-0500 Database 1[87636:7766359] Could not cast value of type '__NSArrayM' (0x7fff87c50980) to 'FIRDocumentReference' (0x10f6d87a8).
Getting closer!
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument(completion: { document, error in
if let err = error {
print(err.localizedDescription)
return
}
let imageUpload = document?["imageUpload"] as? NSArray ?? [""]
print("First Object \(imageUpload.firstObject!)")
})
This prints: First Object <FIRDocumentReference: 0x600001a4f0c0>
Here are two screenshots to help illustrate what the Firestore database looks like..
Ultimately, I need to get to the file field within fl_files. How do I access this from the imageUpload DocumentReference?


Here's some sample code that shows how to read and print out any of the fields values and also how to read the imageUpload field (an array) and print out the first element's value.
I've included both ways to read data from a document because I believe it answers both parts of the question: how to get the array field imageUpload and then access the first element within that array and how to get the value of the file field within fl_files.
Assuming this points to the correct document:
this code will read two fields from the document being pointed to: imageUpload (an array) and fl_id (a String)
Edit:
It appears the objects stored in the imageUpload array are references not strings. As an example of how to access them... here's the code