I am following a tutorial online that uses the older version of Next.js. I am now using Next.js 13, and tried to implement GetStaticProp() to fetch data from my api, but cannot use that function under the app folder.
I am now trying to implement the data fetching as Next.js suggests to do it (https://beta.nextjs.org/docs/data-fetching/fetching). The error that I am getting is:
Unhandled Runtime Error Error: Cannot read properties of undefined (reading 'slice')
My code is:
import Layout from "components/Layout.js";
import ResourceHighlight from "components/ResourceHighlight.js";
import ResourceList from "components/ResourceList.js";
import Newsletter from "components/Newsletter.js";
import Footer from "components/Footer.js";
function Home({resources}) {
return (
<Layout>
<ResourceHighlight
resources={resources.slice(0,2)}
/>
<ResourceList
resources={resources.slice(2)}
/>
<Newsletter />
<Footer />
</Layout>
)
}
export async function getData(){
const resData = await fetch('http://localhost:3000/api/resources"');
const data = await resData.json();
//return resData.json();
return {
props: {
resources: data
}
}
}
// export async function getStaticProps() {
// const resData = await fetch("http://localhost:3000/api/resources");
// const data = await resData.json();
// return {
// props: {
// resources: data
// }
// }
// }
export default Home;
Any help is appreciated. Thanks in advance!
I tried to implement the new function with guidance from the next.js documentation but something is not working.