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

Next.js13+Prisma中findUnique的where属性触发TS错误ts(18004)

问题解决:TypeScript错误 "No value exists in scope for the shorthand property 'email'"

错误原因

你在调用prisma.user.findUnique时使用了对象属性简写email,但当前函数作用域内并没有声明名为email的变量。你的邮箱值实际来自请求体的body.email,但你既没有将其提取为单独变量,也没有在where对象中明确指定对应值,导致TypeScript报错。

修复方案

有两种简单的修复方式:

方式1:明确指定属性值

直接在where对象中使用body.email作为值:

const userWithEmail = await prisma.user.findUnique({
  where: {
    email: body.email,
  },
});

方式2:提前解构请求体

在函数开头从请求体中解构出需要的字段,后续即可使用属性简写:

export async function POST(request: Request) {
  const body = await request.json();
  // 解构出所需字段
  const { email, firstName, lastName, phone, city, password } = body;
  const errors: string[] = [];

  // 验证逻辑也可以改用解构后的变量,提升可读性
  const validationSchema = [
    {
      valid: validator.isLength(firstName, { min: 1, max: 20 }),
      error: "Invalid first name.",
    },
    {
      valid: validator.isEmail(email),
      error: "Invalid email.",
    },
    // ...其他验证项
  ];

  // ...中间逻辑代码

  const userWithEmail = await prisma.user.findUnique({
    where: {
      email, // 现在可正常使用简写
    },
  });
  
  return NextResponse.json({ response: "OK" });
}

额外提示

你的验证逻辑中存在拼写错误:"Invalaid password."应修正为"Invalid password."

内容的提问来源于stack exchange,提问作者Ethan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 16:47:30