#Prisma Client Validation Error

27 messages · Page 1 of 1 (latest)

fallow kraken
#

Running an e2e test on my nest API using Prisma to manage the database connections I got the following error on terminal:

PrismaClientValidationError:
Invalid `this.prisma.user.update()` invocation in
/Users/daniel/DocumentosMac/backend/nestjs-api-tutorial/src/user/user.service.ts:9:41

  6 export class UserService {
  7   constructor(private prisma: PrismaService) {}
  8   async editUser(userId: number, dto: EditUserDto) {
→ 9     const user = await this.prisma.user.update({
          where: {
            id: {
              id: 19,
              createdAt: new Date("2024-03-05T11:20:28.678Z"),
              updatedAt: new Date("2024-03-05T11:20:28.678Z"),
              email: "[email protected]",
              firstName: null,
              lastName: null
            }
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          },
          data: {
            firstName: "Daniel",
            email: "[email protected]"
          }
        })

Argument `id`: Invalid value provided. Expected Int, provided Object......

I think the issue is on id 19 times, but no Idea on what to do next. If you curious about the rest of the code in test file and user.service.ts is here: https://pastebin.com/3WsN7uv9

nimble widget
#

The issue is probably in your controller.

fallow kraken
#

Consotroller follows as that:

import { Body, Controller, Get, Patch, UseGuards } from '@nestjs/common';
import { JwtGuard } from '../auth/guard';
import { GetUser } from '../auth/decorator';
import { User } from '@prisma/client';
import { EditUserDto } from './dto';
import { UserService } from './user.service';

@UseGuards(JwtGuard)
@Controller('users')
export class UserController {
  constructor(private userService: UserService) {}
  @Get('me')
  getMe(@GetUser() user: User) {
    return user;
  }

  @Patch()
  editUser(@GetUser('id') userId: number, @Body() dto: EditUserDto) {
    return this.userService.editUser(userId, dto);
  }
}
nimble widget
#

Getting closer, @GetUser decorator? It's probably returning the whole user instead of just id.
I'd just change it like this anyway:

  @Patch()
  editUser(@GetUser() user: User, @Body() dto: EditUserDto) {
    return this.userService.editUser(user.id, dto);
  }

So you reuse the User type wherever you work with the GetUser decorator. This way, if you ever change id from number to string, you'll get type errors correctly. As you can see, decorators like these are not good at type safety.

fallow kraken
#

Let me try and brb

#

ty

#

I am getting the same error, always pointing towards:

 const user = await this.prisma.user.update({
#

in user.service.ts

#

id keeps growing with each test lol

fallow kraken
#

I can't keep working without knowing this lmao 🤣

nimble widget
#

Well, just follow the code and error.
You see there's the whole user object instead of just it's id un the userService.
That means, where you call the editUser method, you provide incorrect value to the first argument.
Go there and check what you are passing in. If you updated the code based on my recommendation, did you also change the editUser(user.id, dto) call with the .id? If so, there's something else wrong. I can't tell you where the error is, it's your code, you have to follow it to the root cause.

fallow kraken
#

Thanks a lot, I will try it soon and reach out.

fallow kraken
#

The problem is clear, userId must be an integer, but its returning an objet, I've been a cpuple days researching and doing trials, but I could not resolve the error.

I did this in user.service to verify the error:

async editUser(
    userId: number,
     dto: EditUserDto,
   ) {
    if (typeof userId !== 'number') {
        throw new Error('userId must be an integer.');
      }
    const user = await this.prisma.user.update({
      where: {
        id: userId,
      },
      data: {
        ...dto,
      },
    });

But still throwing this error on and on.

I can provide any code you need if you are willing to help me out, because I do not know hot to follow.

Thanks to anyone reading.

#

I get this console err, if helps:

[Nest] 88972  - 07/03/2024, 12:03:18   ERROR [ExceptionsHandler] userId must be an integer.
Error: userId must be an integer.
    at UserService.editUser (/Users/daniel/DocumentosMac/backend/nestjs-api-tutorial/src/user/user.service.ts:15:15)
    at UserController.editUser (/Users/daniel/DocumentosMac/backend/nestjs-api-tutorial/src/user/user.controller.ts:27:29)
    at /Users/daniel/DocumentosMac/backend/nestjs-api-tutorial/node_modules/@nestjs/core/router/router-execution-context.js:38:29
    at /Users/daniel/DocumentosMac/backend/nestjs-api-tutorial/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    at /Users/daniel/DocumentosMac/backend/nestjs-api-tutorial/node_modules/@nestjs/core/router/router-proxy.js:9:17
nimble widget
#

Let's see the src/user/user.controller.ts

fallow kraken
# nimble widget Let's see the `src/user/user.controller.ts`

Sure, here you go:

import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  UseGuards,
} from '@nestjs/common';
import { JwtGuard } from '../auth/guard';
import { GetUser } from '../auth/decorator';
import { User } from '@prisma/client';
import { EditUserDto } from './dto';
import { UserService } from './user.service';

@UseGuards(JwtGuard)
@Controller('users')
export class UserController {
  constructor(private userService: UserService) {}
  @Get('me')
  getMe(@GetUser() user: User) {
    return user;
  }
  
  @Patch()
  editUser(@GetUser('id') userId: number, @Body() dto: EditUserDto) {
    return this.userService.editUser(userId, dto);
  }

}
#

the imports un-needed are bc I did some test on @Patch like:

@Patch(':id')
  editUser(
    @GetUser('id', ParseIntPipe) userId: number,
    @Body() dto: EditUserDto,
  ) {
    return this.userService.editUser(userId, dto);
  }
nimble widget
#

Where's the change I suggested before?

  @Patch()
  editUser(@GetUser() user: User, @Body() dto: EditUserDto) {
    return this.userService.editUser(user.id, dto);
  }
fallow kraken
#

I tried but still getting errors

nimble widget
#

Same errors?

#

Then let's debug from here.

  @Patch()
  editUser(@GetUser() user: User, @Body() dto: EditUserDto) {
    console.log('user in controller:', user); // add console log
    return this.userService.editUser(user.id, dto);
  }

Add a console log and tell me what output you get in the console

fallow kraken
#

I fixed it now with this, also the bookmarKs controller where I was having same issues:

@Patch()
editUser(@GetUser('id') user: User, @Body() dto: EditUserDto) {
  return this.userService.editUser(user.id, dto);
}

//AND FOR THE BOOKMARKS
@Get()
getBookmarks(@GetUser('id') user: User) {
  return this.bookmarkService.getBookmarks(user.id);
}
#

Whats the different between using userId and the user.id formula?

sleek muskBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.

fallow kraken
nimble widget
#

I believe your @GetUser('id') decorator was supposed to return the 'id' value from the user object. From the looks of it, no matter whether you use @GetUser('id') or @GetUser(), it always returns the whole user object.

Given that, you can't just pass the value to the editUser service call. The method expects id of type number. You have user of type User (object). So you pass the .id property of your user, which contains the expected value.