我怎么只能阅读当前的用户评论?对于“我自己的评论”页面。我的代码运行,但如果(where field: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
的所有集合中使用单个查询执行上述操作。