Hi there,
I’m finding some weird behaviour when trying to get content from a Custom Post Type (case_studies). When I start the site with npx frontity dev I get a “Cannot read property of 89” where 89 is the ID of the post type. If I run in the console frontity.state.source and then refresh the page, it loads fine and no more error shows.
It might be that the post type has not been fetched yet and the ID is not ready.
I have added the post type under postTypes in frontity.settings.js
"state": {
"source": {
"url": "http://wwt.local",
"homepage": "/home",
"postTypes": [
{
"type": "case_studies",
"endpoint": "case_studies",
"archive": "/case_studies"
}
]
},
}
Then I get the data in a component like this
const caseStudies = content.case_studies;
{caseStudies && caseStudies.map((caseStudy, index) => {
const caseStudyType = caseStudy.post_type;
useEffect(() => {
actions.source.fetch("/case_studies");
state.source.get("/case_studies/");
}, []);
const caseStudyData = state.source[caseStudyType][caseStudy.ID]; <--- here is where it breaks
const title = caseStudyData.title.rendered;
const client = caseStudyData.acf.client;
const link = libraries.source.normalize(caseStudyData.link);
return (
<CaseStudy key={index}>
<CaseStudyInner>
<CaseStudyClient><b>{client}</b></CaseStudyClient>
<Link link={link}><CaseStudyTitle>{title}</CaseStudyTitle></Link>
</CaseStudyInner>
</CaseStudy>
);
})}
I’m pretty new to React and Frontity, any help would be really appreciated…! Thank you