【问题标题】:How to get document id after creation Firestore ios?创建 Firestore ios 后如何获取文档 ID?
【发布时间】:2020-08-19 00:31:53
【问题描述】:

Firestore 的新手。有没有办法获取新创建的文档的 ID 或引用?

firestore.collection(myCollection).document().setData(["myData": data]) { err in
   if let err = err {
      print("Error writing document: \(err)")
   } else {
      print("Document successfully written!")
   }
}

非常感谢任何形式的帮助。

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore


    【解决方案1】:

    您可以在文档创建之前获取它,因为它是在您创建DocumentReference 时在客户端上生成的。

    let ref = firestore.collection(myCollection).document()
    // ref is a DocumentReference
    let id = ref.documentID
    // id contains the random ID
    ref.setData(...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-03
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2019-02-19
      • 1970-01-01
      • 2020-12-04
      相关资源
      最近更新 更多