Reputation: 273
as we now NextJS released their update to have access to api's within the APP directory and I've had trouble getting prisma to work with it. I can get prisma to work fully functional with pages/api, but not with app/api. Wondering if there was anyone that has managed to get this to work yet. If so, any tips would be greatly appreciated.
I tried to replicate the same structure from pages/api -> app/api (with the new route handlers) with NextJS new APP directory API.
Sample of what I am trying to do.
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export default async function handle(req:any, res:any) {
const { firstname, lastname, dateofbirth, email, password } = req.body;
const addUser: any = await prisma.owners.create({
data: {
firstname: firstname,
lastname: lastname,
dateofbirth: dateofbirth,
email: email,
password: password,
},
});
res.json(addUser)
}
Upvotes: 2
Views: 1832
Reputation: 1180
Since you are creating a user (owner) in your example, I assume that your old endpoint was in the following file: /pages/api/owners.ts
The new endpoint could be created with the following structure: /app/api/owners/route.ts
and could look like this with the new Route Handlers:
// create user
export async function POST(req: Request) {
// assuming your body has json data
const { firstname, lastname, dateofbirth, email, password } = await req.json();
const addUser: any = await prisma.owners.create({
data: {
firstname: firstname,
lastname: lastname,
dateofbirth: dateofbirth,
email: email,
password: password,
},
});
return Response.json(addUser);
}
Upvotes: 2