Search code examples
node.jstypescriptnestjsprisma

Prisma throws error on findUniqueByEmail request, and it refers for some reason to findUniqueById request


I'm a newbie in Prisma, and I'm confused with the findUniqueOrThrow method, where I'm trying to get user record by unique email value. For some reason, the error I get after sending a request to this endpoint refers to a other findUniqueOrThrow method, but with an id selection. Can someone help, how can if fix it?

schema.prisma

model User {
  id        Int       @id @default(autoincrement())
  firstName String?
  lastName  String?
  email     String?   @unique
  password  String?
  phone     String?
  roles     Role[]    @default([CUSTOMER])
  isBanned  Boolean?  @default(false)
  comments  Comment[]
  token     String?
  createdAt DateTime? @default(now())
  updatedAt DateTime? @updatedAt
}

user.service.ts

@Injectable()
export class UserService {
  constructor(private prisma: PrismaClient) {}

    async findUserByEmail(email: string): Promise<User> {
    try {
      const user = await this.prisma.user.findUniqueOrThrow({
        where: {
          email: email,
        },
      });
      return user;
    } catch (e) {
      console.log(e);
    }

user.controller.ts

@Get('user/findByEmail')
  async getUserByEmail(@Body() data: EmailDto) {
    return await this.userService.findUserByEmail(data.email);
  }

Error:

error


Solution

  • Route order in your user controller matters. Make sure you have the GET route to get user by email above the GET route for getting user by id.