Invalid `prisma.user.findUnique()` invocation:

288 Views Asked by At

on below my code for API route

export const POST = async (req: NextRequest) => {
...
  try {
    const { email, name, password } = await req.json();
    console.info(email, name, password);
    const existingUser = await prismadb.user.findUnique({
      where: {
        email,
      },
    });
    if (existingUser) {
      return NextResponse.json({ status: 422, error: "Email taken" });
    }
    const hashedPassword = await bcrypt.hash(password, 12);
    const user = await prismadb.user.create({
      data: {
        email,
        name,
        hashedPassword,
        image: "",
        emailVerified: new Date(),
      },
    });
   ...
};

on below my code for schema.prisma

model User{
..
email String? @unique
...
}

i was defined @unique on email, but still get error

✓ Compiled /api/register/route in 468ms (332 modules) [email protected] test 123 PrismaClientKnownRequestError: Invalid prisma.user.findUnique() invocation: how i can fix this problem? please help

1

There are 1 best solutions below

0
On

Note that because you added unique modifier there is no possibility to make it optional. IOW write it like that

email String @unique

You can refer to this documentation.