nestjs-api-tutorial icon indicating copy to clipboard operation
nestjs-api-tutorial copied to clipboard

Argument where of type UserWhereUniqueInput needs at least one argument

Open edson-martins opened this issue 2 years ago • 0 comments

Issue Description

The strategy validate method is using only id in the where clause but when doing the call for /users/me it getting the error:

[Nest] 41843  - 04/11/2023, 8:49:30 PM   ERROR [ExceptionsHandler] 
Invalid `this.prisma.user.findUnique()` invocation in
/Users/edsonmartins/dev/nest/freeCodeCamp/GHTA143_b-s/nestjs-api-tutorial/src/auth/strategy/jwt.strategy.ts:17:41

  14   });
  15 }
  16 async validate(payload: { sub: number; email: string }) {
→ 17   const user = await this.prisma.user.findUnique({
         where: {
       ?   id?: Int,
       ?   email?: String
         }
       })

Argument where of type UserWhereUniqueInput needs at least one argument. Available args are listed in green.

Note: Lines with ? are optional.

Looking to solve this, I have changed my local code as:

async validate(payload: { sub: number; email: string }) {
    const user = await this.prisma.user.findUnique({
      where: {
        id: payload.sub,
        email: payload.email,
      },
    });
    delete user.hash;
    return user;
  }

or using findFirst instead of using findUnique.

User Prisma schema

model User {
  id Int @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  email String @unique
  hash String

  firstName String?
  lastName String?

  bookmarks Bookmark[]

  @@map("users")
}

Could you share if the approach using findUnique with both fields or findFirst with id only is correct or is there something more to use findUnique with only the id?

edson-martins avatar Apr 11 '23 23:04 edson-martins