【问题标题】:prisma findUnique with where throws an error棱镜 findUnique 与 where 引发错误
【发布时间】:2021-08-29 06:29:27
【问题描述】:

我正在尝试在 ExpressJS 和 Prisma 中创建 Singup API。

我正在检查我的数据库中是否存在给定的电子邮件。当我传递电子邮件和密码时,它会抛出Unknown arg email in where.email for type SignupWhereUniqueInput. Did you mean id? Available args: 错误。

我尝试使用 select: { email: true, password: true } 但它不起作用。只有当我传递身份证号码而不是电子邮件时,代码才有效。任何想法这里有什么问题??

router.post(`/signup`, async (req, res) => {
  const { email, password } = req.body;

  const hashPassword = await bcrypt.hash(password, 10);

  const checkEmail = await prisma.signup.findUnique({
    where: {
      email: email
    }
  });

  if (checkEmail) {
    return res.status(400).json({
      error: "Pick different one"
    });
  }
  const userSignUp = await prisma.signup.create({
    data: {
      email,
      password: hashPassword
    }
  });

  return res.json(userSignUp);
});

// DB Schema
model Signup {
  id       Int    @id @default(autoincrement())
  email    String
  password String
}

【问题讨论】:

    标签: javascript node.js express prisma


    【解决方案1】:

    您收到此错误的原因是您的 Prisma Signup 模型中的 email 字段不是唯一findUnique 查询中的 where 选项仅接受模型的 unique 字段。这样做是为了确保findUnique 查询只能指定一条记录。如果您有兴趣了解更多信息,Prisma Client API reference 会深入解释此行为。

    要解决该错误,请在您的 Prisma Schema 文件中使 email 字段唯一。更新后的Signup 模型应如下所示:

    model Signup {
      id       Int      @id @default(autoincrement())
      email    String   @unique      
      password String 
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2021-12-05
      • 2019-09-22
      • 1970-01-01
      相关资源
      最近更新 更多