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 不返回所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)