new to next.js trying to fetch data
Unanswered
Asian black bear posted this in #help-forum
Asian black bearOP
I have a file named db.json and I'm trying to parse it with a fetch request but it's failing. I'm trying to create a job board as a beginner project. The getJobs() function is async and I'm awaiting the response. Is there anyone who can help me?
6 Replies
Plott Hound
follow this guide https://vercel.com/guides/loading-static-file-nextjs-api-route
tldr this:
import { promises as fs } from 'fs';
export default async function Page() {
const file = await fs.readFile(process.cwd() + '/app/data.json', 'utf8');
const data = JSON.parse(file);
return (
<div>
<h1>{data.title}</h1>
<p>{data.content}</p>
</div>
);
}
Asian black bearOP
ty
I will try this out
Plott Hound
no worries. let me know how you got on
Ray
you could import the json file instead