Skip to content

Just ignore fields in request when model is marked strict and do not throw error. #7206

Answered by marioestradarosa
mevivek asked this question in Q&A
Discussion options

You must be logged in to vote

@mevivek , you should remove the id before calling the create repository method.

Assuming you have the id as follows:

export class User extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,
  })
  id?: number;

Then your controller should look like this:

async create(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(User, {
            title: 'NewUser',
          }),
        },
      },
    })
    user: User,
  ): Promise<User> {
    delete user.id;
    return this.userRepository.create(user);
  }

Note that I removed the exclude: ['id'],. from the @requestBody decorator and the Omit<User, 'id'> was replace…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@marioestradarosa
Comment options

@mevivek
Comment options

@marioestradarosa
Comment options

Answer selected by mevivek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants