unable to get Firebase documentID

ello, i'm running a query to Firebase and i need to get the document ID of the collection. This is the code to run the query:


Variable


private var ptListInCell = [PTList]()



@IBAction func getDataTapped(_ sender: Any) {
        SVProgressHUD.show()
        
        if HOSP != (hospnameTxt.text!) {
            ptListQuery = ptListCollectionRef?.whereField("hosp", isEqualTo: (hospnameTxt.text!))
        }
        
        ptListQuery?.getDocuments { (snapshot, error) in
            if let err = error {
                debugPrint("error getting data: \(err)")
            } else {
                guard let snap = snapshot else { return }
                for document in snap.documents {
                    let data = document.data()
                    let ptName = data[PTNAME] as? String ?? ""
                    let assignedMd = data[ASSIGNEDMD] as? String ?? ""
                    let officeMd = data[OFFICEMD] as? String ?? ""
                    let assignedDate = data[ASSIGNEDDATE] as? String ?? ""
                    let seeNoSee = data[SEENOSEE] as? String ?? ""
                    let room = data[ROOM] as? String ?? ""
                    let app = data[APP] as? String ?? ""
                    let documentId = document.documentID
                    print("documentId", documentId)
     
                    let newPtList = PTList(ptName: ptName, assignedMd: assignedMd, officeMd: officeMd, assignedDate: assignedDate, seeNoSee: seeNoSee, room: room, app: app, documentId: documentId)
                    print("newPtList", newPtList)
                    print("documentId", documentId)
                    
                    self.ptListInCell.append(newPtList)
                }
            }

it pulls everything but the documentID. When i print to the console:


print(ptlist?.documentId asAny)


Result is nil.

thank you!

Accepted Reply

Test line 26 of your first post:


print("newPtList", newPtList)



Also check:


line 25 is ptlsits


self.ptlists.append(newPtList)


but you log ptlist


print(ptlist?.documentId asAny)


So check everywhere ptlist and ptlists to see what is the correct one.

Where and how did you define plist

Whare and how did you define plists

Replies

First, can you test, line 23, the value of documentId.

     print("documentId", documentId)


Second, could you test ptlist for nil ?


add

     print("ptlist", ptlist)
     print(ptlist?.documentId)

before

     print(ptlist?.documentId asAny)

here are the results:


documentId bjTfYQqWIOFF6fbX652k

ptlist nil

nil


ID shows up but does not attach to the array. Here is the definition of PTList.



class PTList {
   
    private(set) var ptname: String!
    private(set) var assignedmd: String!
    private(set) var officemd: String!
    private(set) var assigneddate: String!
    private(set) var seenosee: String?
    private(set) var room: String?
    private(set) var app: String!
    private(set) var documentId: String!
   
    init(ptname: String, assignedmd: String, officemd: String, assigneddate: String, seenosee: String, 
         room: String, app: String, documentId: String) {
       
        self.ptname = ptname
        self.assignedmd = assignedmd
        self.officemd = officemd
        self.assigneddate = assigneddate
        self.seenosee = seenosee
        self.room = room
        self.app = app
        self.documentId = documentId
    }
}

Test line 26 of your first post:


print("newPtList", newPtList)



Also check:


line 25 is ptlsits


self.ptlists.append(newPtList)


but you log ptlist


print(ptlist?.documentId asAny)


So check everywhere ptlist and ptlists to see what is the correct one.

Where and how did you define plist

Whare and how did you define plists

What are the answers to my questions ?


What is plist ? I understand ptlists is now ptListInCell.


Did you modify the original post ? That makes it pretty hard to follow.