Next.js Discord

Discord Forum

Create json file dont work

Unanswered
Ojos Azules posted this in #help-forum
Open in Discord
Ojos AzulesOP
This is my code import fs from "fs"; import path from "path"; import { NextApiRequest, NextApiResponse } from "next"; import prisma from "@/prisma/lib/prisma"; export default async function handler( req: NextApiRequest, res: NextApiResponse ) { try { const data = await prisma.barber.findMany(); // Define the file path where you want to save the JSON file const filePath = path.join(process.cwd(), "barbers.json"); // Convert the data to JSON string const jsonData = JSON.stringify(data, null, 2); // Write the JSON data to the file fs.writeFileSync(filePath, jsonData); return res.status(200).json(data); } catch (error) { return res.status(500).json(error); } } I want to get the prisma data and pass to json file so to display data in components, but for some reason doesnt create file "barbers.json". Any help? If you need more information you can tell me. I use App Directory

1 Reply

Ojos AzulesOP