Typeorm 不返回所有数据

Typeorm Does not return all data(Typeorm 不返回所有数据)
本文介绍了Typeorm 不返回所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了无法获取 Typeorm 返回的所有数据的问题.

I have an issue where I could not get all of the data returned by Typeorm.

这里是相关实体.

媒体实体:

@Entity()
export class Media {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('text')
  type: string;
}

类别实体:

@Entity()
export class Category {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToMany((type) => Model, (model) => model.category)
  models: Model[];

  @ManyToMany(() => Media, { cascade: true })
  @JoinTable()
  medias: Media[];
}

模型实体:

@Entity()
export class Model {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  manufacturer: string;
....
  @ManyToOne(() => Category, (category) => category.models)
  category: Category;

  @ManyToMany(() => Media, { cascade: true })
  @JoinTable()
  medias: Media[];
}

当我执行这段代码时:

 const getOneCategory: Category = await this.categoryRepository
        .createQueryBuilder('category')
        .leftJoinAndSelect('category.medias', 'media')
        .leftJoinAndSelect('category.models', 'model')
        .getOne();

它回来了

{
    "id": 1,
    "name": "Luxury",
    "medias": [
      {
        "id": 1,
        "type": "image",
        "url": "images/car-categories/luxury.png"
      },
      {
        "id": 2,
        "type": "the boy on fireeeeeeee",
        "url": "httpezdths:/whjrbfkjaberkjPokemonssss"
      }
    ],
    "models": [
      {
        "id": 2,
        "manufacturer": "Lamborghini1",
        "model": "Aventador1",
        "shortName": "Aventador1",
        "zeroToHundred": "3.53",
        "transmission": "AMT1",
        "driveTrain": "AWD1",
        "topSpeed": 3203,
        "engine": "2.9L1"
// THE MEDIA ATTRIBUTE is MISSING
      },
    ]
  }

模型对象键中缺少 media 属性.我想像这样获取 model 对象键中的媒体数组

The media attribute is missing from the model object key. I would like to get the array of media in the model object key like this

...

models: [{
      "id": 1,
      "manufacturer": "Lamborghini1",
      "model": "Aventador1",
      "shortName": "Aventador1",
      "zeroToHundred": "3.53",
      "transmission": "AMT1",
      "driveTrain": "AWD1",
      "topSpeed": 3203,
      "engine": "2.9L1",
      "medias": [ //I WANT TO GET THIS VALUE AS WELL
        {
          "id": 3,
          "type": "jasretdhsdfl1",
          "url": "httpezdths://wsrg1111111"
        }
      ]
    },]

...

感谢任何帮助.

推荐答案

通过添加模型和媒体之间的连接,您可以获得想要的结果:

You get the wanted result by adding the join between model and media :

 const getOneCategory: Category = await this.categoryRepository
    .createQueryBuilder('category')
    .leftJoinAndSelect('category.medias', 'media')
    .leftJoinAndSelect('category.models', 'model')
    .leftJoinAndSelect('model.medias', 'medias') // this line 
    .getOne();

这篇关于Typeorm 不返回所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SQL Server version of MySQL#39;s group_concat and escaped strings(MySQL 的 group_concat 和转义字符串的 SQL Server 版本)
Can#39;t set root MySQL password to null(无法将 root MySQL 密码设置为 null)
Unable to start the mysql server in ubuntu(无法在 ubuntu 中启动 mysql 服务器)
Working with Nodejs + MSSQL at Linux / Ubuntu(在 Linux/Ubuntu 上使用 Nodejs + MSSQL)
MySQL won#39;t start after changin my.cf(更改 my.cf 后 MySQL 无法启动)
Unable to start MySQL server - Control process exited with error code(无法启动 MySQL 服务器 - 控制进程退出并显示错误代码)