1

我遇到了一个问题,我需要通过用户名和电子邮件检查用户是否存在,因为两者都是数据库中的唯一字段,但出现错误。 Argument where of type UserWhereUniqueInput needs exactly one argument, but you provided username and email. Please choose one. 那么,有没有办法只执行一次这个查询?而不是像下面这样为每个运行一个

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

而不是这样

const user = await prisma.user.findUnique({
        where: {
          username,
        },
      });

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

4

3 回答 3

3

我不完全确定,但棱镜返回一个 JS 对象......所以也许这样的查询应该工作:

const query = await prisma.user.findUnique({
        where: {
          user: user.username
        },
       select: {
         user: true,
         email: true
      }
      });

我相信这应该可行,但它仍然只能找到一个独特的。我不确定您为什么会选择用户+电子邮件作为唯一的,因为我会假设一个无论如何都与另一个相关联。

如果我的解决方案无法工作,我会在这里查看更多答案:https ://www.prisma.io/docs/concepts/components/prisma-client/crud#findfirst

于 2021-02-25T03:06:46.587 回答
0
const users = await prisma.user.findMany({
       where: {
        OR: [
          {username},
          {email}
        ]
      }
  });
 if (users.length !== 0) {...}
于 2021-08-15T17:22:31.410 回答
0

如果您正在寻找一个可以为您带来单一结果的独特价值,您也可以使用 findFirst。这会给你对象而不是数组。即使您正在寻找一个唯一值,findMany 也会返回一个数组。

const users = await prisma.user.findFirst({
 where: {OR: [{username},{email}]}
});
于 2021-12-04T19:35:58.940 回答