I have an Astro site that uses Contentful as a CMS.
I want to fetch data from Contentful to display on the website. I've been following the guidance on the Astro site.
lib/contentful.ts
import contentful, { type EntryFieldTypes, type Asset } from "contentful";
export interface homepage {
contentTypeId: "page",
fields: {
introductionTitle: EntryFieldTypes.Text
introductionSubtitle: EntryFieldTypes.Text
heroImage: Asset
}
}
Then display the results on index.astro
import { contentfulClient } from "../lib/contentful";
import type { homepage } from "../lib/contentful";
const entries = await contentfulClient.getEntries<homepage>({
content_type: "page",
});
const pages = entries.items.map((item) => {
const { introductionTitle, introductionSubtitle, heroImage } = item.fields;
return {
introductionTitle,
introductionSubtitle,
heroImage
};
});
This works great on the development server, however when I go to build the site locally or deploy the site to Vercel I get the following error:
▶ src/pages/index.astro └─ /index.htmlCannot read properties of undefined (reading 'fields')