本文介紹了Typeorm 不返回所有數(shù)據(jù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我遇到了無(wú)法獲取 Typeorm 返回的所有數(shù)據(jù)的問(wèn)題.
I have an issue where I could not get all of the data returned by Typeorm.
這里是相關(guān)實(shí)體.
媒體實(shí)體:
@Entity()
export class Media {
@PrimaryGeneratedColumn()
id: number;
@Column('text')
type: string;
}
類(lèi)別實(shí)體:
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@OneToMany((type) => Model, (model) => model.category)
models: Model[];
@ManyToMany(() => Media, { cascade: true })
@JoinTable()
medias: Media[];
}
模型實(shí)體:
@Entity()
export class Model {
@PrimaryGeneratedColumn()
id: number;
@Column()
manufacturer: string;
....
@ManyToOne(() => Category, (category) => category.models)
category: Category;
@ManyToMany(() => Media, { cascade: true })
@JoinTable()
medias: Media[];
}
當(dāng)我執(zhí)行這段代碼時(shí):
const getOneCategory: Category = await this.categoryRepository
.createQueryBuilder('category')
.leftJoinAndSelect('category.medias', 'media')
.leftJoinAndSelect('category.models', 'model')
.getOne();
它回來(lái)了
{
"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
},
]
}
模型對(duì)象鍵中缺少 media
屬性.我想像這樣獲取 model
對(duì)象鍵中的媒體數(shù)組
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"
}
]
},]
...
感謝任何幫助.
推薦答案
通過(guò)添加模型和媒體之間的連接,您可以獲得想要的結(jié)果:
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();
這篇關(guān)于Typeorm 不返回所有數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!