简体   繁体   中英

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.

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"] });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM