Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 204
Anas Avatar asked Mar 23 '26 01:03

Anas


1 Answers

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.

like image 55
CyberMessiah Avatar answered Mar 25 '26 16:03

CyberMessiah