在开发 Next.js 项目。运行 npm run build 时,构建失败并出现以下错误:
Type error: Type '{ __tag__: "GET"; __param_position__: "second"; __param_type__: { params: { id: string; }; }; }' does not satisfy the constraint 'ParamCheck<RouteContext>'.
The types of '__param_type__.params' are incompatible between these types.
Type '{ id: string; }' is missing the following properties from type 'Promise<any>': then, catch, finally, [Symbol.toStringTag]
错误上下文: 导致问题的文件:app/api/projects/[id] 中的route.ts。 相关代码:这是似乎导致问题的 GET 处理程序的片段:
从“next/server”导入 { NextResponse };
import {
prisma } from "@/lib/prisma";
export async function GET(
request: Request,
{
params }: {
params: {
id: string } }
) {
try {
const project = await prisma.project.findUnique({
where: {
id: params.id },
});
if (!project) {
return NextResponse.json({
error: "Project not found" }, {
status: 404 });
}
return NextResponse.json(project);
} catch (error) {
console.error("Error fetching project:", error);
return NextResponse.json({
error: "Failed to fetch project" }, {
status: 500 });
}
}
如何修复
因为在 Next.js 15 中,params 是一个promise对象
以下是解决此问题的方法:
export async function GET(
request: Request,
params: {
params: Promise<{
id: string }> }
) {
const id = await params.id
// rest of your code
}