Reputation: 1755
I have a few documents with the following collection structure in 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"
im trying to loop thru my documents and render its collection data to my components but im having issues.
my query
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);
});
that query outputs the above collection from firestore
. thanks for the help
Upvotes: 2
Views: 149
Reputation: 31315
Get your querySnapshot
and map it to result in an array of objects containing your documents.
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);
});
Then you can do something like:
// 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>
);
Upvotes: 1