需要从文档访问 collection Firestore

Need acces from document to collection Firestore

我正在尝试做一个 iOS 应用程序,我已经将它与 firebase 绑定,所以我正在尝试获取一些帖子的广告来获取它们,这工作正常,但是这个帖子有 2 collections(点赞和回复),我正在尝试获取点赞,问题是我无法获得点赞,因为出于某些原因,我无法为文档 forEach 提供 class,我也不能访问它,有人知道吗?

代码:

import Foundation
import Firebase

struct Post : Hashable {
    var id : String
    var dateAdded : String
    var posterEmail : String
    var posterUsername : String
    var posterIcon : String
    var postTitle : String
    var postBody : String
    var likes : [String]
    var userLikedPost : Bool
}

struct Like {
    var likeId : String
    var likerEmail : String
}

class Likes {
    var likes : [Like] = []
    
    func fetchLikes() {
        //Firestore.firestore()
    }
}


class Posts : ObservableObject {
    @Published var posts : [Post] = []
    
    func fetchPosts() {
        Firestore.firestore().collection("posts").getDocuments(completion: { (docPosts, error) in
            if (error != nil) {
                print("error fetching posts")
            } else {
                docPosts?.documents.forEach { (post) in
                    let id = post.documentID
                    let email = post.get("posterEmail") as! String
                    let username = post.get("posterUsername") as! String
                    let icon = post.get("posterIcon") as! String
                    let title = post.get("title") as! String
                    let body = post.get("body") as! String

                    // Here i want to insert the code that gets the likes class and access the likes variable
                    
                    self.posts.append(Post(id: id, dateAdded:String(id.split(separator: "_").joined(separator: "/").prefix(10)) ,posterEmail: email, posterUsername: username, posterIcon: icon, postTitle: title, postBody: body,
                                           likes: [],userLikedPost: false))
                }
            }
        })
    }
}

Firestore 结构没有包含在问题中,所以我将提供一个供使用

user_wines
   uid_0
      name: "Jay"
      favorite_wines:
         0: "Insignia"
         1: "Scavino Bricco Barolo"
         2: "Lynch Bages"
   uid_1
      name: "Cindy"
      favorite_wines
         0: "Palermo"
         1: "Mercury Head"
         2: "Scarecrow"

然后是读取所有用户文档、获取名称、酒单(作为字符串数组)并将其输出到控制台的代码

func readArrayOfStrings() {
    let usersCollection = self.db.collection("user_wines")
    usersCollection.getDocuments(completion: { snapshot, error in

        guard let allDocs = snapshot?.documents else { return }

        for doc in allDocs {
            let name = doc.get("name") as? String ?? "No Name"
            let wines = doc.get("favorite_wines") as? [String] ?? []
            wines.forEach { print("   ", [=11=]) }
        }

    })
}

和输出

Jay
   Insignia
   Scavino Bricco Barolo
   Lynch Bages
Cindy
   Palermo
   Mercury Head
   Scarecrow

编辑

这是使用 Codable 的相同代码

class UserWineClass: Codable {
   @DocumentID var id: String?
   var name: String
   var favorite_wines: [String]
}

和将数据读入class

的代码
for doc in allDocs {
    do {
        let userWine = try doc.data(as: UserWineClass.self)
        print(userWine.name)
        userWine.favorite_wines.forEach { print("   ", [=14=]) }
    } catch {
        print(error)
    }
}