我已经嵌套了要在tableView中显示的数据。

我的数据结构像这样...

/users
    /userid
         name: "John"
         age: 23
         /likedPosts
            0:post1
            1:post2

对于tableview,我想显示这些帖子(它们具有自己的数据收集)。

为此,我需要...

1)获取数组的数量并

2)查询用户LikedPost数组值以获取帖子的内容。

我目前正在使用getDocument函数,无法弄清楚。

例如...
func getUserLikedPosts() {
    if let user = Auth.auth().currentUser {
        let userFS = Firestore.firestore().collection("users").document(user.uid)
        userFS.getDocument(completion: { (snapshot, error) in
            print(snapshot?)

        })
    }
}

这甚至不打印出嵌套数组吗?

最佳答案

这样的事情应该起作用:

func getUserLikedPosts() {
if let user = Auth.auth().currentUser {
    let userFS = Firestore.firestore().collection("users").document(user.uid).collection(“likedPosts”)
    userFS.getDocuments(completion: { (snapshot, error) in
        print(snapshot?)

    })
}
}

使用Firestore,您需要向下钻取到所需的实际节点,这与可以访问子快照的Firebase不同。

因此,在经过快速讨论后,与OP一起讨论的是相关用户没有Likedest对象。

要访问阵列(请注意,我尚未测试此代码,这是一个示例):
If let doc = document, let array = doc[“likedPost] as NSArray {
print(array)
}

07-24 09:36
查看更多