Issue with fetching data in Next.js using getServerSideProps
18 replies
Last updated: Jan 7, 2022
B
I have a question about fetching data for next.js, Im using getServerSideProps
and then passing it into my page
when I console log the data I’m seeing the data I expect but when I use it in my page nothing shows up. If I console log just the
export const getServerSideProps = async function (context) { const data = await client.fetch( ` *[_type == "page" && _id == "frontpage"] { _id, pageDescription, pageTitle, pageBuilder, mainImage } ` ) return { props: { data }, } }
export default function Home({ data }) { console.log(data) return( <h1>{data.pageTitle}</h1> ) }
data.pageTitleit returns undefined . Any idea why my
data.pageTitleis undefined?
console.log(data) [ { _id: 'frontpage', mainImage: { _type: 'image', asset: [Object] }, pageBuilder: [ [Object], [Object], [Object], [Object] ], pageDescription: [ [Object] ], pageTitle: 'Frontpage' } ]
Jan 7, 2022, 1:30 PM
S
You’re currently fetching an array, of 1 item. If you only expect to be fetching 1 page at a time, I would definitely recommend refactoring the query into the following:
The
You can read more about this, in the following section:
https://www.sanity.io/docs/query-cheat-sheet#170b92d4caa2
*[_type == "page" && _id == "frontpage"][0] {...}
[0]indicates, that you want to retrieve the first item within the array.
You can read more about this, in the following section:
https://www.sanity.io/docs/query-cheat-sheet#170b92d4caa2
Jan 7, 2022, 1:33 PM
B
for this instance I do only want to fetch data from the page with the _id of frontpage, is there a better way to do this?
Jan 7, 2022, 1:35 PM
S
Then you’re doing it the right way, if you just add the
[0]to the query
Jan 7, 2022, 1:36 PM
B
Great thanks for the help
Jan 7, 2022, 1:37 PM
B
hmm seems like now its not recognizing pageTitle at all
Jan 7, 2022, 1:39 PM
B
ReferenceError: pageTitle is not defined 16 | <main className="flex flex-col items-center justify-center w-full flex-1 px-20 text-center"> 17 | <h1 className="text-6xl font-bold"> > 18 | {pageTitle} | ^ 19 | </h1> 20 | 21 | <p className="mt-3 text-2xl">
Jan 7, 2022, 1:39 PM
S
Can you try logging the data, that you parse to your props?
Jan 7, 2022, 1:39 PM
B
{ _id: 'frontpage', mainImage: { _type: 'image', asset: { _ref: 'image-b85cee75c4f5940699368a39e3ba3d4eb9d236b4-2678x3349-jpg', _type: 'reference' } }, pageBuilder: [ { _key: '5f3774a9f249', _type: 'hero', heading: "Brian and Kates' Bakery", image: [Object], tagline: 'Fresh baked goods everyday' }, { _key: 'ad2c5ddfc4fa', _type: 'callToAction', linkText: 'Order Now', url: '<https://google.com/>' }, { _key: '1e1a091c36fa', _type: 'gallery', images: [Array] }, { _key: '9095e53a3bc7', _type: 'paragraph', copy: 'This is paragraph text' } ], pageDescription: [ { _key: '4521deeccf43', _type: 'block', children: [Array], markDefs: [], style: 'normal' } ], pageTitle: 'Frontpage' }
Jan 7, 2022, 1:43 PM
B
the logged data
Jan 7, 2022, 1:43 PM
S
Are you destructuring the data object that you parse the props, to get
pageTitle- else you would have to use the
data.pageTitle
Jan 7, 2022, 1:44 PM
B
I’ve done it both ways and both are returning pageTitle as undefined
Jan 7, 2022, 1:46 PM
B
even though the log shows it properly
Jan 7, 2022, 1:46 PM
S
Can you send over your getServerSideProps function again?
Jan 7, 2022, 1:47 PM
B
export const getServerSideProps = async function (context) { const data = await client.fetch(` *[_type == "page" && _id == "frontpage"][0] { _id, pageDescription, pageTitle, pageBuilder, mainImage } `) return { props: { data }, } }
Jan 7, 2022, 1:50 PM
S
And how does your Home component look?
Jan 7, 2022, 1:51 PM
B
I dont know what I did other than restart the terminal but now it works
Jan 7, 2022, 1:52 PM
B
lol thanks for all the help
Jan 7, 2022, 1:52 PM
S
Sometimes all it takes, is just a restart apparently haha - no worries
Jan 7, 2022, 1:53 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.