是否有任何解决方案可以修复 NextResponse.json() 以使用 status:200 ,它拒绝在 nextjs 14 中编译?

问题描述 投票:0回答:1

这是我有的一个简单的 api 路由:

// import prisma from "../../../utils/db";
import {NextRequest, NextResponse} from "next/server";

const addresses = [
    {
        id: 1,
        name: "John Doe",
        email: "[email protected]",
        phone: "0123456789",
        address: "123 Main Street, Anytown, USA",
        city: "Anytown",
        state: "USA",
        zip: "12345",
        country: "USA"
    },
    {
        id: 2,
        name: "John Doe",
        email: "[email protected]",
        phone: "0123456789",
        address: "123 Main Street, Anytown, USA",
        city: "Anytown",
        state: "USA",
        zip: "12345",
        country: "USA"
    }
];



export async function GET(request: NextRequest) {
    return NextResponse.json(addresses, {status: 200});

}

问题是 {status: 200} 与 NextResponse.json(....) 一起使用时永远不会编译,它与 Response.json(....) 一起使用时可以正常工作。 我在文档中读到了一些示例,他们将 status:200 与 NextResponse.json 一起使用!

我阅读了文档,他们使用:

import { NextResponse } from 'next/server'
 
export async function GET(request: Request) {
  return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
}

我在 IntellJ IDEA 中复制了相同的代码块,并且再次出现“{ status: 500 }”无法编译,总是给出此错误: “参数类型 {status: number} 不可分配给参数类型 ResponseInit | undefined”

仅当我使用 Response.json 而不是 NextResponse.json 时才有效

api next.js status httpwebresponse
1个回答
0
投票

我的错,它工作正常。 问题是我的 IDE(IntellJ IDEA)用红色下划线作为编译错误“status:200”,即使出现此错误,我也没有想到尝试或测试代码,并且它工作正常。 我仍然不知道为什么 IntellJ IDEA 一直显示错误! 谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.