'SwiftUI - How to add sub-collection + document to existing document in Firestore

I'm trying to figure out how to add a new sub-collection + document to an already existing document in Firestore. Here's a quick idea of the db:

orgs <-Collection
  Acme <-Document (need to grab documentID from here and pass to function)
    employees <-Sub-Collection
      Marge Simpson <-Document
      Homer Simpson <-Document
  Acme2 <-Document

The basic idea is:

  • The user is presented with a list of company names.
  • User clicks on Acme, is presented with a list of employees that work for Acme.
  • There will be some way to add a new employee to Acme.
  • This will likely be a "Add Employee" button on the navigation bar that opens a sheet with text fields where you enter employee information.
  • When that data is saved, it should save to a new document under orgs > Acme > Employees

My current code, when run, will add a new doc at orgs/ blank doc /employees. I can't figure out how to grab the document ID of the org I'm currently looking at, in this case Acme, and pass that org document ID to the function so it adds the new employee to the correct org (Acme in this example).

Here's my view models:

Org View Model:

class OrgViewModel: ObservableObject {
    @Published var orgs = [Org]()
    @Published var newOrg: Org
    
    init(newOrg: Org = Org(orgName: "", orgCity: "")) {
        self.newOrg = newOrg
    }
    
    private var db = Firestore.firestore()

    func fetchOrgData() {
        db.collection("orgs").addSnapshotListener { (querySnapshot, error) in
            guard let documents = querySnapshot?.documents
            else {
                print("No Documents")
                return
            }

            self.orgs = documents.compactMap { (queryDocumentSnapshot) -> Org? in
                return try? queryDocumentSnapshot.data(as: Org.self)
            }
        }
    }
}

Employee View Model:

class EmployeeViewModel: ObservableObject {
    @Published var employees = [Employee]()
    @Published var newEmployee: Employee
    
    init(newEmployee: Employee = Employee(firstName: "", lastName: "", orgName: "")) {
        self.newEmployee = newEmployee
    }
    
    private var db = Firestore.firestore()

    func addEmployeeData(newEmployee: Employee) {
        do {
            let orgRef = db.collection("orgs").document() // <--How do I pass org documentID here??
            let _ = try orgRef.collection("employees").addDocument(from: newEmployee)
        }
        catch {
            print(error)
        }
    }
    
    func fetchEmployeeData() {
        db.collectionGroup("employees").addSnapshotListener { (querySnapshot, error) in
            guard let documents = querySnapshot?.documents
            else {
                print("No Documents")
                return
            }

            self.employees = documents.compactMap { (queryDocumentSnapshot) -> Season? in
                return try? queryDocumentSnapshot.data(as: Employee.self)
            }
        }
    }
}

This needs to be able to read an existing documentID from an existing org and pass that documentID to addEmployeeData function. I'm new to Swift, so any help is greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source