So I'm trying to get all documents from a specific collection from Firestore, but when stepping through the code, it skipped over the line and doesn't throw an error. I've used the exact same code in other parts of my app with success but it is not working here for some reason? Any help would be appreciated.
func getClientEmail() -> String? {
var clientEmail: String?
self.db.collection("Clients").getDocuments() { (querySnapshot, err) in
if let err = err {
print("error getting docs: \(err)")
} else {
for document in querySnapshot!.documents {
let result = Result {
try document.data(as: User.self)
}
switch result {
case .success(let client):
if let client = client {
if client.placementIDs[0] == self.user?.placementIDs[0] {
clientEmail = client.email
}
} else {
print("Document doesn't exist")
}
case .failure(let error):
print("Error decoding user: \(error)")
}
}
}
}
return clientEmail
}
After some testing, I moved this code to within the viewDidLoad() function and it worked... So I think it has something to do within it being wrapped in a function but not sure why, hope this information is helpful to anyone that might be able to fix this problem.