英文:
NextJS 13 Beta App Directory API with Prisma
问题
由于我现在无法执行代码,所以无法确认特定的解决方案。但是,你可以尝试以下方法来解决你的问题:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export default async function handle(req, res) {
    if (req.method === "POST") {
        try {
            const { firstname, lastname, dateofbirth, email, password } = req.body;
            const addUser = await prisma.owners.create({
                data: {
                    firstname: firstname,
                    lastname: lastname,
                    dateofbirth: dateofbirth,
                    email: email,
                    password: password,
                },
            });
            res.status(201).json(addUser);
        } catch (error) {
            console.error("Error adding user:", error);
            res.status(500).json({ error: "Failed to add user" });
        } finally {
            await prisma.$disconnect();
        }
    } else {
        res.status(405).json({ error: "Method Not Allowed" });
    }
}
请注意,这是一个基本的示例,用于处理POST请求。你可能需要根据你的具体需求进行调整。同时,确保你的路由配置和请求方法正确。
英文:
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)
}
答案1
得分: 2
由于您在示例中正在创建一个用户(所有者),我假设您的旧端点位于以下文件中:/pages/api/owners.ts
新的端点可以按以下结构创建:/app/api/owners/route.ts,并且可能如下所示,使用新的路由处理程序:
// 创建用户
export async function POST(req: Request) {
  // 假设您的请求体包含 JSON 数据
  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);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论