如何从 collection firestore 访问文档属性。反应

How to access documents properties from collection firestore. reactjs

这是我的代码:

const posts = firestore.collection("posts");

    function myFunc() {
        posts.get().then((result) => {
            result.docs.forEach((post) => {
                console.log(post.poster);
            })
        })
    }

当我运行这个函数时,当在实际文档中海报属性设置为字符串变量时,它会为每个文档记录未定义。

要从 firestore 访问 collection/document 数据,您需要在遍历文档结果时调用函数“.data()”。

我已经更新了下面的代码,它将控制台记录来自 'posts' 集合中每个文档的数据对象中键 'poster' 的字符串值。

const posts = firestore.collection("posts");

    function myFunc() {
        posts.get().then((result) => {
            result.docs.forEach((post) => {
                console.log(post.data().poster);
            })
        })
    }