3

I am trying to get the document id from Firestore by executing a query like this:-

func updateStatusInFirestore() {

let orderid = saleOrder.first?.Orderid ?? ""
print(orderid)
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings
 self.db.collection("SaleOrders").whereField("orderid", isEqualTo: "\(orderid)").getDocuments { (snapshot, err) in

    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in snapshot!.documents {
            self.documentid = document.documentID
            print(self.documentid)
        }
    }
}

}

In which I am getting the order id from my model class and it is printing the value of order id but when I am trying to put it in whereField query it is not exectuing the query and I am not getting any result in my console.

If I use like this it is working

self.db.collection("SaleOrders").whereField("orderid", isEqualTo: "ji20190205091948").getDocuments

but when I use like this

let orderid = saleOrder.first?.Orderid ?? ""
self.db.collection("SaleOrders").whereField("orderid", isEqualTo: "\(orderid)").getDocuments

It is not working. What is wrong I am doing. Please help?

Wings
  • 2,398
  • 23
  • 46

1 Answers1

5

I Solved the problem. We just need to add one if condition to get the documentId of that particular collection from Firestore

for document in snapshot!.documents {

  if document == document {
   print(document.documentID)
     }
       }
Wings
  • 2,398
  • 23
  • 46