简体   繁体   中英

Cannot get documents from Firestore in Swift

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.

Thanks to Jay's comment, I managed to fix the problem by having clientEmail as a global variable and using a completion handler to assign the value in this function.

func getClientEmail() {
        // Get email of client for corresponding contractor
        db.collection("Clients").getDocuments(completion: { (querySnapshot, err) in
            if let err = err {
                print(err.localizedDescription)
                return
            } 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] {
                                self.clientEmail = client.email
                            }
                        } else {
                            print("Document doesn't exist")
                        }
                    case .failure(let error):
                        print("Error decoding user: \(error)")
                    }
                }
            }
        })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM