I am trying to display the data read from my firestore collection, but after getting the data from firestore, I tried using a map function to map & display the data accordingly but it ends up with an error:
TypeError: Cannot read property 'map' of undefined
Which may indicate that my events are in a wrong format for the map function or the events are empty.
Here is my code for more clarification:
const [events, setEvents] = React.useState();
// const events = [];
React.useEffect(() => {
db.collection("Events").get()
.then(querySnapshot => {
// const data = querySnapshot.docs.map(doc => doc.data());
let events = [];
querySnapshot.forEach(doc =>
events.push({ ...doc.data() })
)
console.log(events);
setEvents(events);
});
});
return (
<Grid item sm={4}>
{ events.map(event => (
<Card className={classes.card}>
<CardHeader
avatar={
loading ? (
<Skeleton variant="circle" width={40} height={40} />
) :
<Avatar
alt="Ted talk"
src={event.eventImgUrl}
/>
}
.... // code contiues
I expect the code to display data for each event accordingly