Get document id From Firestore Swift

假装没事ソ 提交于 2020-05-16 21:59:52

问题


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?


回答1:


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)
     }
       }


来源:https://stackoverflow.com/questions/54628601/get-document-id-from-firestore-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!