我如何才能只阅读当前用户的评论?对于“我自己的评论”页面。我的代码已运行,但如果(其中字段: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/