You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署NextJS项目时Prisma类型错误排查求助

部署NextJS+Prisma项目时的TypeError问题

错误信息

Type error: Type '{ title: any; price: any; content: any; author: { connect: { email: string; }; }; }' is not assignable to type '(Without<PostCreateInput, PostUncheckedCreateInput> & PostUncheckedCreateInput) | (Without<...> & PostCreateInput)'.
  Object literal may only specify known properties, and 'price' does not exist in type '(Without<PostCreateInput, PostUncheckedCreateInput> & PostUncheckedCreateInput) | (Without<...> & PostCreateInput)'.

相关代码

API路由代码(pages/api/post/index.ts)

export default async function handle(req, res) {
  const { title, content, price } = req.body;

  const session = await getSession({ req });
  const result = await prisma.post.create({
    data: {
      title: title,
      price: price,
      content: content,
      author: { connect: { email: session?.user?.email } },
    },
  });
  res.json(result);
}

Prisma Schema(schema.prisma)

model Post {
  id        String  @id @default(cuid())
  title     String
  content   String?
  price     String
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  String?
}

前端提交组件代码

const Draft: React.FC = () => {
  const [title, setTitle] = useState("");
  const [content, setContent] = useState("");
  const [price, setPrice] = useState("");

  const submitData = async (e: React.SyntheticEvent) => {
    e.preventDefault();
    try {
      const body = { title, content, price };
      await fetch("api/post", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body),
      });
      await Router.push("/drafts");
    } catch (error) {
      console.error(error);
    }
  };
}

问题分析

本地运行正常但部署报错,核心原因是部署环境中Prisma生成的客户端类型定义未包含新增的price字段。即便本地执行过npx prisma generate,如果部署流程中没有同步执行该命令,或者依赖缓存了旧版本的Prisma客户端,就会出现类型不匹配的错误。

解决方案

  1. 修改package.json的build脚本,确保生成Prisma客户端在NextJS构建之前执行:
    "scripts": {
      "build": "npx prisma generate && next build",
      "dev": "next dev",
      "start": "next start"
    }
    
  2. 清理部署环境的依赖缓存,确保重新安装依赖时生成最新的Prisma客户端:
    • 删除部署服务器上的node_modules文件夹和package-lock.json/yarn.lock
    • 重新执行npm install或yarn install
  3. 确认schema.prisma文件已同步到部署环境,检查服务器上的schema文件是否包含price字段,避免部署时遗漏文件。

内容的提问来源于stack exchange,提问作者Clément Ramos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 20:03:26