【问题标题】:How can I return only one attribute of a typeorm relation?如何只返回 typeorm 关系的一个属性?
【发布时间】:2022-01-22 02:32:25
【问题描述】:

这是我返回所有视频的方法,当我发出请求时,除了返回视频之外,它还返回类别对象,但我只想返回类别名称而不是整个对象。

async listAll(request: Request, response: Response): Promise<Response> {
    const repo = getRepository(Video);
    const videos = await repo.find({
        relations: ['category']
    });
    return response.status(200).json(videos);
}

我的视频模型具有以下结构:

    @Column()
    category_id!: string;

    @ManyToOne(() => Category)
    @JoinColumn({ name: "category_id" })
    category!: Category

【问题讨论】:

    标签: javascript node.js api express typeorm


    【解决方案1】:

    也许你可以这样做

    const videos = await repo.find({
              select: ["name_category","id_category"],
              relations: ["category"],
            });
    

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多