Igor Bezlepkin
Igor Bezlepkin

Reputation: 57

How do I return only the required fields in NestJs?

I'm trying to get all the records from the table.

controller:

  @Get('types')
  async getTypes(): Promise<PageTypeRO[]> {
      return this.pageService.findTypes();
  };

service:

 async findTypes(): Promise<PageTypeRO[]> {
     return await this.pageTypePropsRepository.find();
 }

interface (RO):

export interface PageTypeRO {
    readonly id: number
}

I expect to get an array with objects in which only the "id" field, but teach all the fields from the table.

Upvotes: 0

Views: 1921

Answers (1)

Ayoub Touba
Ayoub Touba

Reputation: 3007

You have to set columns you want to get, To make it work for you, you should edit FindTypes function:

async findTypes(): Promise<PageTypeRO[]> {
    return await this.pageTypePropsRepository.find({ select: ["id"] });
}

Upvotes: 2

Related Questions