Nextjs Typescript 在 api 路由中解构参数时出错

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

当我构建它时,我有一个下一个项目,它在本地运行良好,但无论我尝试什么,在 vercel 上都会抛出此错误

Type error: Route "[action]/route.ts" has an invalid "POST" export:
  Type "NextResponse<unknown>" is not a valid type for the function's second argument.
Error: Command "npm run build" exited with 1

这是我当前的代码,我已经尝试了它的许多变体,但它不允许我构建

declare module "next/server" {
  interface NextResponse {
    params: { action: string }; // Add your custom property here
  }
}
export const POST = async (req: NextRequest, res: NextResponse) => {
  const { params } = await res;

  if (!params || !params.action)
    return NextResponse.json({ success: false, message: "Incorrect usage" });

  const action = params.action;

  
  ...

也尝试过扩展

interface MyResponse extends NextResponse {
  params: { action: string }; // Add your custom property here
}
export const POST = async (req: NextRequest, res: MyResponse) => {

任何帮助将不胜感激

javascript typescript next.js app-router
1个回答
0
投票

使用

next build
构建它会在本地得到相同的错误。

路由处理程序与 App Router 具有不同的签名,请参阅官方文档。 如果您依赖您的回复,请尝试按如下方式等待请求:

export const POST = async (req: NextRequest) => {
  const data = await req.json();
  const { params } = await data as MyResponse;

  if (!params || !params.action)
    return NextResponse.json({ success: false, message: "Incorrect usage" });

  const action = params.action;
  ...
};
© www.soinside.com 2019 - 2024. All rights reserved.