从Firestore Swift获取文档ID [英] Get document id From Firestore Swift

查看:54
本文介绍了从Firestore Swift获取文档ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过执行如下查询来从 Firestore 中获取文档ID:-

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

}

我从模型类中获取订单ID,并在其中打印订单ID的值,但是当我尝试将其放入 whereField 查询时,它不执行查询,因此我在我的控制台上没有任何结果.

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

但是当我这样使用

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?

推荐答案

我解决了问题.我们只需要添加一个 if 条件,即可从 Firestore

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

这篇关于从Firestore Swift获取文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆