NextJS 13 Beta 应用程序目录 API 与 Prisma

huangapple go评论55阅读模式
英文:

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);
}

huangapple
  • 本文由 发表于 2023年3月1日 14:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定