swift - Firestore : How i can read subcollection data with documentID?

标签 swift firebase google-cloud-firestore

我如何才能只阅读当前用户的评论?对于“我自己的评论”页面。我的代码已运行,但如果(其中字段:currentuid)不存在则不打印(“无数据”)。只打印 ("documents.data()")

    let db = Firestore.firestore()
    db.collection("tweets").getDocuments { (snapshot, error) in
        for document in snapshot!.documents {
            self.db.collection("tweets").document(document.documentID).collection("comments").whereField("uid", isEqualTo: self.uid).getDocuments(completion: { (snapshot2, error2) in
                for document2 in snapshot2!.documents {
                    print("\(document2.documentID) -> \(document2.data())")
                }
            })
        }
    }

最佳答案

我想你正在寻找这个:

let db = Firestore.firestore()
db.collection("tweets").getDocuments { (snapshot, error) in
    for document in snapshot!.documents {
        self.db.collection("tweets").document(document.documentID).collection("comments")
          .whereField("uid", isEqualTo: self.uid).getDocuments(completion: { (snapshot2, error2) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                if snapshot2.isEmpty {
                    print("No comments from \(self.uid) found")
                } else {
                    for document2 in snapshot2!.documents {
                        print("\(document2.documentID) -> \(document2.data())")
                    }
                }
            }
        })
    }
}

请注意,集合组查询的工作正在进行中,这将允许您在所有名为 comments 的集合中使用单个查询执行上述操作。

关于swift - Firestore : How i can read subcollection data with documentID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611180/

相关文章:

java - Android 通知栏中不显示推送通知

ios - Firebase 通知仅显示在控制中心

使用 Firebase 在库模块中进行 Android 仪器测试

javascript - React 不执行组件中的函数

ios - DispatchSourceTimer 在 deinit/dealloc 中崩溃

ios - UITabbar 在 iOS 10 中拉伸(stretch)但在 11+ 中不拉伸(stretch)

javascript - QueryDocumentSnapshot 似乎缺少其父类的方法

firebase - 如何编写以Text为值的条件语句?

ios - TouchesBegan 总是返回错误的 UIView

ios - 自动在其他应用程序中插入文本 - swift4