0


I'm giving my first steps in Loopback 4, and I've been trying to follow the tutorial that they have to generate a new REST Api.
The thing is that after creating my model, database and repository, I started to develop my controller class and encountered a piece of code that nowhere in the documentation we have a mention to it.

I have it signaled below:

async createTodo(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Todo, {title: 'NewTodo', exclude: ['id']}),
        },
      },
    })
 todo: **Omit<Todo, 'id'>,** <----------
  ): Promise<Todo> {
    ...
    return this.todoRepository.create(todo);
  }

My question is: For what this omit is for? I've tried to remove it from the code, and the result is the same as the one that I have without it.

Also, in that post request, I'd like to hide the id property from the object, when I receive the answer. How can I do that just for this request' response?

Thanks in advance!

Salitha
  • 1,022
  • 1
  • 12
  • 32
RafDias
  • 21
  • 1
  • 7

1 Answers1

1

This is more Typescript thing than Loopback.

Loopback uses Typescript so you usually define types and interfaces. For controller request body, you need to define interface.

Omit<T,K>

Constructs a type by picking all properties from T and then removing K.

Here, I assume you have set id property to auto generate or generate by yourself. And id property is not a user input. Your front-end does not send id value. Then You need to create an interface with all properties of TodoModel except id. Now you can create new record with the received object.

You can read more on Typescript utilities here

In addition, Loopaback4 uses DataObject<Model> to convert model into interface.

Salitha
  • 1,022
  • 1
  • 12
  • 32