简体   繁体   English

如何仅返回 NestJs 中的必填字段?

[英]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):接口(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.我希望得到一个包含对象的数组,其中只有“id”字段,但会教表中的所有字段。

You have to set columns you want to get, To make it work for you, you should edit FindTypes function:您必须设置要获取的列,要使其适合您,您应该编辑 FindTypes 函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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