gpt4 book ai didi

javascript - 使用 Firestore 文档和收集数据

转载 作者:行者123 更新时间:2023-11-29 22:55:40 25 4
gpt4 key购买 nike

我在 Firestore 中有一些具有以下集合结构的文档

_fl_meta_: Object { createdBy: "Kz5vTvzlmGhRCxqKuDhrvvANqPe2", docId: "76qlSYWItERvJVnLKSDt", env: "production", … }
author: "asd"
content: "last 1"
date: "2019-06-19T12:00:00-07:00"
id: "76qlSYWItERvJVnLKSDt"
imageDeck: Array [ {…} ]
order: 0
parentId: 0
status: "published"
summary: "last 1"
title: "last 1"

我试图遍历我的文档并将其集合数据呈现给我的组件,但我遇到了问题。

我的查询

const db = firebase.firestore();
db
.collection('fl_content')
.where('_fl_meta_.schema', '==', 'blog')
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(doc.data());
// let results = Object.keys(doc.data()).map(function (key) {
// return [String(key), doc.data()[key]];
// })
// this.setState({ cards: results });
});
})
.catch(error => {
console.log("Error getting documents: ", error);
});

该查询从 firestore 输出上述集合。谢谢你的帮助

最佳答案

获取您的querySnapshot 并将其映射以生成包含您的文档的对象数组。

const querySnapshot = firebase.firestore().collection('fl_content')
.where('_fl_meta_.schema', '==', 'blog').get()

.then((querySnapshot) => {
// The following line will result in an array of objects (each object is your document data)
const yourDocuments = querySnapshot.docs.map((doc) => doc.data());
this.setState({cards: yourDocuments});
})
.catch((error) => {
console.log(error);
});

然后你可以这样做:

// INSIDE YOUR RENDER METHOD

const cardItems = this.state.cards.map((item,index) =>
<CardComponent key={index}>
{item.name} // Here you can access and display the fields of your documents
</CardComponent>
);

return(
<SomeContainerComponent>
{cardItems}
</SomeContainerComponent>
);

关于javascript - 使用 Firestore 文档和收集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56698385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com