Skip to content

Argument where of type UserWhereUniqueInput needs at least one argument #13

@edson-martins

Description

@edson-martins

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions