Error serializing props returned from `getServerSideProps` in "/[slug]" when accessing a page.

39 replies
Last updated: Jan 25, 2022
Why isn’t my [slug].js page displaying when going to the page?I keep getting this error

error - Error: Error serializing props returned from `getServerSideProps` in "/[slug]".
Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }` (received: `[object Null]`).
This is what my getServerSideProps looks like

export const getServerSideProps = async function (context) {
    const { slug = '' } = context.query;
  
    const data = await client.fetch(`*[_type == "hikes" && slug.current == $slug][0] `, { slug });
    return { props: data };
};
I’ve used this ^ structure on a different slug page and it works
Jan 25, 2022, 10:01 PM
Subtle, but Next.js wants the props data to be an object
Jan 25, 2022, 10:03 PM
It’s still pushing me to a 404
Jan 25, 2022, 10:04 PM
What is the console/terminal saying?
Jan 25, 2022, 10:04 PM
error - Error: Error serializing props returned from `getServerSideProps` in "/[slug]".
Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }` (received: `[object Null]`).
Jan 25, 2022, 10:05 PM
Can you verify that you’re getting data in
data
 ?
Jan 25, 2022, 10:05 PM
I can not, it wont even let me console log
Jan 25, 2022, 10:05 PM
const data = await client.fetch(`*[_type == "hikes" && slug.current == $slug][0] `, { slug });
console.log(data)
so this doesn’t work? have you restarted the dev server?
Jan 25, 2022, 10:06 PM
I’ll try restarting
Jan 25, 2022, 10:06 PM
Now its not giving me the error but still pushing me to a 404
Jan 25, 2022, 10:08 PM
And you’re not getting any data?
Jan 25, 2022, 10:08 PM
correct
Jan 25, 2022, 10:09 PM
import client from '../lib/sanity'

export default function Hikes({ data }) {
    console.log(data)
    return(
        <main>
            
        </main>
    )
}

export const getServerSideProps = async function (context) {
    const { slug = '' } = context.query;
  
    const data = await client.fetch(`*[_type == "hikes" && slug.current == $slug][0] `, { slug });
    console.log(data)
    return { props: { data } };
};
Jan 25, 2022, 10:09 PM
Is it
hikes
or
hike
 ?
Jan 25, 2022, 10:09 PM
Does the query work in Vision?
Jan 25, 2022, 10:09 PM
I dont know how to see it in vision I dont have that on my studio
Jan 25, 2022, 10:10 PM
Got ya. It’s a GROQ playground, so can be useful.
sanity install @sanity/vision
Jan 25, 2022, 10:11 PM
What’s your project ID?
Jan 25, 2022, 10:11 PM
cd0q2c94
Jan 25, 2022, 10:11 PM
Thanks. So I can confirm that
client.fetch(`*[_type == "hikes" && slug.current == $slug][0]`, { slug: "2022-01-24" })
should return data. I find a bit weird that you’re getting a 404 though. Even if there no data, the page template should’ve returned those empty
<main>
. This is a file in the
pages
folder, right?
Jan 25, 2022, 10:17 PM
Correct, I have a hike folder in the pages folder. Inside the /hike is an index and [slug]
Jan 25, 2022, 10:18 PM
index shows
Jan 25, 2022, 10:18 PM
I did write that wrong, but even after correcting, it still send me to 404
Jan 25, 2022, 10:21 PM
Okay, thanks for confirming.
Jan 25, 2022, 10:21 PM
Jan 25, 2022, 10:24 PM
Even copying and pasting that sends me a 404
Jan 25, 2022, 10:27 PM
I’m sorry for the trouble
Jan 25, 2022, 10:27 PM
not at all! Do you have the code in a github or something?
Jan 25, 2022, 10:29 PM
Yea I’ll push what I have and make it public so you can see it
Jan 25, 2022, 10:30 PM
Ah! Found it. Rename your folder from
hike.js
 to
hike
Jan 25, 2022, 10:34 PM
(Also, love that you’re making a dog journal 💯)
Jan 25, 2022, 10:35 PM
haha yup that worked, I see .js so often I missed that
Jan 25, 2022, 10:36 PM
and thanks! I watched something that said it could improve your relationship with your dog (once you realize what you do with them everyday) and at the very least you have a story of them at the end of their life
Jan 25, 2022, 10:37 PM
Totally understandable – it’s way easier for me to spot stuff like that coming in fresh
Jan 25, 2022, 10:37 PM
Looking forward to learning more about the site once you have launced it and told i-made-this 😄
Jan 25, 2022, 10:37 PM
I do have it already deplyed https://dog-journal.vercel.app/
I’m committing to the
52 Hike challenge which is why I’m now building the hike part of the site
Jan 25, 2022, 10:39 PM
user G
I was just reading this for nerd reasons initially but that is a gorgeous dog.
Jan 25, 2022, 10:49 PM
😍 This is a fantastic project - on all accounts. (
user G
, have you seen this?)
Jan 25, 2022, 11:01 PM
😍 This is a fantastic project - on all accounts. (
user G
, have you seen this?)
Jan 25, 2022, 11:01 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?