[英]How to get every post inside nested database from firebase?
Hello there I have nested database with collection(quotes)>document(uid)>collection(quote)>document(id)你好,我有嵌套数据库 collection(quotes)>document(uid)>collection(quote)>document(id)
When I try to fetch the quote, I can only fetch for current user.当我尝试获取报价时,我只能为当前用户获取。 How can I loop through uid and get everything inside quote collection for every user.我如何遍历 uid 并为每个用户获取报价集合中的所有内容。
My code for fetching the quotes:我获取报价的代码:
func fetchQuote() {
guard let uid = Auth.auth().currentUser?.uid else {
return
}
Firestore.firestore().collection("quotes")
.document(uid).collection("quote")
.addSnapshotListener { querySnapshot, error in
if let error = error {
print("There was an error while fetch the quotes.")
return
}
querySnapshot?.documentChanges.forEach({ change in
if change.type == .added{
let data = change.document.data()
self.quotes.append(.init(documentId:change.document.documentID, data: data))
}
})
}
}
I tried to remove the following:我试图删除以下内容:
.document(uid).collection("quote")
What I did is use of .collectionGroup()
我所做的是使用.collectionGroup()
Firestore.firestore().collectionGroup("quote").getDocuments(){ querySnapshot, error in
if let error = error {
print("There was an error \(error)")
return
}
querySnapshot?.documentChanges.forEach({ change in
if change.type == .added{
let data = change.document.data()
self.quotes.append(.init(documentId:change.document.documentID, data: data))
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.