Are you please able to look at the following and help me understand why my documents fail to retrieve from Firestore? My code is below:
class ContactStore : ObservableObject {
@Published var datas = [contactDataType]()
private var db = Firestore.firestore()
func fetchData() {
db.collection("Contact Details").addSnapshotListener { (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print("No documents")
return
}
self.datas = documents.compactMap { queryDocumentSnapshot -> contactDataType? in
return try? queryDocumentSnapshot.data(as: contactDataType.self)
}
}
}
}
My struct is as follows
struct contactDataType : Identifiable, Codable {
@DocumentID var id: String? = UUID().uuidString
var adno : String
var fullname : String
var firstname : String
var lastname : String
var registrationgroup : String
var priority1relation : String
var priority1fullname : String
var priority1maintelephone : String
var priority1mobile : String
var priority2relation : String
var priority2fullname : String
var priority2maintelephone : String
var priority2mobile : String
var priority3relation : String
var priority3fullname : String
var priority3maintelephone : String
var priority3mobile : String
var priority4relation : String
var priority4fullname : String
var priority4maintelephone : String
var priority4mobile : String
I am able to retrieve the documents using the following code,
class ContactStore : ObservableObject{
@Published var datas = [contactDataType]()
@AppStorage("selectedSchool") var selectedSchool: String = "selectedSchool"
init() {
let db = Firestore.firestore()
db.collection("School Name/\(selectedSchool/School Assets/\(**Struggling with this bit**)/Contact Details").getDocuments { (snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
for i in snap!.documents{
let id = i.documentID
let adno = i.get("ID") as? String ?? ""
let fullname = i.get("Full Name") as? String ?? ""
let firstname = i.get("First Name") as? String ?? ""
let lastname = i.get("Last Name") as? String ?? ""
let registrationgroup = i.get("Registration Group") as? String ?? ""
let priority1relation = i.get("Priority 1 Relation") as? String ?? ""
let priority1fullname = i.get("Priority 1 Full Name") as? String ?? ""
let priority1maintelephone = i.get("Priority 1 Main Telephone") as? String ?? ""
let priority1mobile = i.get("Priority 1 Mobile") as? String ?? ""
let priority2relation = i.get("Priority 2 Relation") as? String ?? ""
let priority2fullname = i.get("Priority 2 Full Name") as? String ?? ""
let priority2maintelephone = i.get("Priority 2 Main Telephone") as? String ?? ""
let priority2mobile = i.get("Priority 2 Mobile") as? String ?? ""
let priority3relation = i.get("Priority 3 Relation") as? String ?? ""
let priority3fullname = i.get("Priority 3 Full Name") as? String ?? ""
let priority3maintelephone = i.get("Priority 3 Main Telephone") as? String ?? ""
let priority3mobile = i.get("Priority 3 Mobile") as? String ?? ""
let priority4relation = i.get("Priority 4 Relation") as? String ?? ""
let priority4fullname = i.get("Priority 4 Full Name") as? String ?? ""
let priority4maintelephone = i.get("Priority 4 Main Telephone") as? String ?? ""
let priority4mobile = i.get("Priority 4 Mobile") as? String ?? ""
self.datas.append(contactDataType(id: id, adno: adno, fullname: fullname, firstname: firstname, lastname: lastname, registrationgroup: registrationgroup, priority1relation: priority1relation, priority1fullname: priority1fullname, priority1maintelephone: priority1maintelephone, priority1mobile: priority1mobile, priority2relation: priority2relation, priority2fullname: priority2fullname, priority2maintelephone: priority2maintelephone, priority2mobile: priority2mobile, priority3relation: priority3relation, priority3fullname: priority3fullname, priority3maintelephone: priority3maintelephone, priority3mobile: priority3mobile, priority4relation: priority4relation, priority4fullname: priority4fullname, priority4maintelephone: priority4maintelephone, priority4mobile: priority4mobile))
}
}
}
}
**So overall, I'm struggling to either pass the document ID into the path in the second code or retreive my documents using the first. I know it would be better using the first, but I would really like to pass the document ID's into the document path rather than create collection group queries as it doesn't seem to match with would I would like to do.
I understand the naming structure isn't quite right and should avoid spaces**
At this moment in time I have set my rules to read for all so I know this is not a security issue.
Questions -
Am I able to pass the Document ID into a path? I'm not sure and I can't find any documentation on this although I have managed this through AppStorage for Selected School
Is Collection Group Queries the better way to go for this?
Thank you for any and all advice.