I am trying to get data as given solution above but still i am getting undefined instead of data but when I send this same request through postman and thunderclient it gives me success with 200 status code. Please Guys, help me out...
export async function getServerSideProps(context) {
const res = await fetch('http://localhost:5000/api/blogpost/view');
const { data } = await res.json();
return {
props: {
blog: data,
},
}
}
const Page = ({blog}) => {
console.log({ blog: blog, title: "nothing" });
return (
<div><h1>Hello</h3></div>
)}
export default Page;
**I also have tried the fetch request inside try-catch and then also I got same undefined message **
export async function getServerSideProps(context) {
// Fetch data from external API
try {
const res = await fetch('http://localhost:5000/api/blogpost/view');
const { data } = await res.json();
} catch (error) {
console.log(error);
}
console.log(data);
// Pass data to the page via props
return { props: { blog: data } };
}
Console Data that I am getting is same as before
{ blog: undefined, title: 'nothing' }