typescript Next.js API路线给500

pjngdqdw  于 2023-11-20  发布在  TypeScript
关注(0)|答案(1)|浏览(134)
import { Prisma, PrismaClient } from "@prisma/client"
import { NextApiRequest, NextApiResponse } from "next";
import { NextRequest, NextResponse } from "next/server";


const prisma = new PrismaClient();

export async function GET() {
    try {
        const machines = await prisma.machines.findMany();
        console.log(machines, "machines")
        return {
            body: machines,
            status: 200
        };
    } catch (error) {
        console.error(error)
        return {
            body: { error: "An error occurred while fetching the data." },
            status: 500
        };
    }
}

字符串
机器是控制台日志记录正确,但我得到了500没有响应机构时,我试图击中路线。
其他信息:我正在使用应用程序路由器,此文件位于app > API > get > route.ts

uelo1irk

uelo1irk1#

在Next中,如果你想返回数据,最好使用NextResponse

import { NextResponse } from "next/server";

NextResponse.json(data, {
  status: xxx,
})

字符串
查看官方文档:https://nextjs.org/docs/app/api-reference/functions/next-response#json

相关问题