簡體   English   中英

如何從 firebase 獲取嵌套數據庫中的每個帖子?

[英]How to get every post inside nested database from firebase?

你好,我有嵌套數據庫 collection(quotes)>document(uid)>collection(quote)>document(id)

當我嘗試獲取報價時,我只能為當前用戶獲取。 我如何遍歷 uid 並為每個用戶獲取報價集合中的所有內容。

我獲取報價的代碼:

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))
                }
            })
            
   
        }
    }

我試圖刪除以下內容:

.document(uid).collection("quote") 

我所做的是使用.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM