Next.js Discord

Discord Forum

new to next.js trying to fetch data

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Avatar
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

Avatar
Plott Hound
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>
  );
}
Avatar
Asian black bearOP
ty
I will try this out
Avatar
Plott Hound
no worries. let me know how you got on
Avatar
Ray
you could import the json file instead