Unable to get data from getServerSideProps.
Unanswered
Mugger Crocodile posted this in #help-forum
Mugger CrocodileOP
Hi there!
I'm new to nextjs and i'm using getServerSideProps but not getting data returned.
I've used it in Components directory as well in pages directory(as in nextjs docs)
Its always returning me {}.
So, Please help me correct the code tell me about what should i do encounter this issue.
Thanks!
I'm new to nextjs and i'm using getServerSideProps but not getting data returned.
I've used it in Components directory as well in pages directory(as in nextjs docs)
export async function getServerSideProps() {
return { props: { data: "dummmy data" } };
}
export default function Page(props) {
console.log(props);
return (
<main>
<p>{props.data}</p>
</main>
);
}Its always returning me {}.
So, Please help me correct the code tell me about what should i do encounter this issue.
Thanks!
18 Replies
From what i understand you can only use it in a page
And also you said your new right- are you using the app or page routers?
Mugger CrocodileOP
@Netherland Dwarf im using app
Netherland Dwarf
Oh okay
Hmm im not sure if getServerSideProps is supported in the app directory
Because in next version 12 you would use these functions to make components into server components
But in next 14 every component by default is a server component
Mugger CrocodileOP
@Netherland Dwarf when i use it in app it gives me error that it can't be used in app.
that's why i used it in components directory then component but there no data is returned only {}
that's why i used it in components directory then component but there no data is returned only {}
if you have recently used it can u show me where to use this code
Netherland Dwarf
@Mugger Crocodile you mean the getServerSideProps
But like i said app directory doesnt support it
The link i sent shows how to do proper fetching in server side
In your /app you have page.js for example
You would fetch data there and pass it down to components as a prop