0
struct StaffAccount: Identifiable {
    var id: String
    var email: String
    var password: String
    var status: String
}

class StaffAccountModel: ObservableObject {
    @Published var model = [StaffAccount]()
    
    func getData() -> String? {
            let db = Firestore.firestore()
            db.collection("staff").getDocuments { snapshot, error in
                if error == nil {
                    if let snapshot = snapshot {
                        DispatchQueue.main.async {
                            self.model = snapshot.documents.map { d in
                                return StaffAccount(id: d.documentID,
                                                    email: d["email"] as? String ?? "",
                                                    password: d["password"] as? String ?? "",
                                                    status: d["status"] as? String ?? "")
                            }
                        }
                    }
                }
                else {
                    //Handle the error
                }
                
            }
            return model.status
    }
}

I want to get status key data which is the same for all users. If I write model[0].status, then the program gives an error after launch and the value is not displayed on the screen. I need that after calling the function, I get the value corresponding to the status key in the firestore. Now my mistake is that I incorrectly prescribe the return

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

0 Answers0