NodeJS 未在Typeorm API上返回导入的实体

cyvaqqii  于 2023-03-17  发布在  Node.js
关注(0)|答案(1)|浏览(139)

我编写这段代码是为了获取与特定国家ID相关联的所有想法,但是当我获取数据时,它没有将我导入的级别和国家实体添加到IdeaEntity中。

export class IdeaEntity extends BaseEntity {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({nullable: true})
    @IsString()
    @IsOptional()
    @Length(7, 9)
    idea_title: string;

    @Column({type: 'enum', enum: LevesStatus, default: LevelsStatus.OPEN})
    levels_status: LevelsStatus;

    @Column({type: 'enum', enum: Payment, default: Payment.NOT_PAID })
    payment_status: Payment;

    @Column({nullable: true, default: null})
    video: string;

    @Column({nullable: true, default: null })
    price: number

    *@ManyToOne(() => LevelsEntity, (level) => level.name, {eager: true})
    level: LevelEntity;

    *@ManyToOne(() => CountryEntity, (country) => country.country_name, {eager: true})
    country: CountryEntity;
} 


const getIdeaCountries = await IdeaRepository.createQueryBuilder('idea')
        .select()
        .where('idea.country = :query', {query: country})
        .getOne();
        console.log(getIdeaCountries?.country);

这是返回的数据,它没有
“理念实体”:{“编号”:“2 e8541 c6-e485- 40 c1-b 0 ed-bfaeb 320 c 021”,“创意标题”:“玩家”、“等级状态”:“已锁定”、“支付状态”:“未付费”、“视频”:“默认. png”,“价格”:五十
这里是级别和国家实体,不知道我错过了什么。

export enum Star {
    STAR = 'star',
    NO_STAR = 'N_star'
  }

export enum Green {
    GREEN = 'green',
    NO_GREEN = 'No_green'
}

@Entity("level")
// @Tree("closure-table")
export class LevelEntity extends BaseEntity {
    @PrimaryGeneratedColumn('uuid')
    id: number;

    @Column()
    name: string;

    @Column({type: 'enum', enum: Star, default: Star.NO_STAR })
    star_status: Star;

    @Column({type: 'enum', enum: Green, default: Green.GREEN })
    green_status: Green;
}

此处为实体

@Entity("country")
  export class CountryEntity extends BaseEntity {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({nullable: true})
    @IsString()
    @IsOptional()
    @Length(7, 9)
    country_name: string;

    @Column({nullable: true, default: null})
    country_flag: string;

    @ManyToOne(() => FieldEntity, (field) => field.id, {eager:true})
    @JoinColumn({name: 'field_id'})
    field: FieldEntity;

    @OneToMany(() => ChallengeEntity, (challenge) => challenge.country)
    challenges: ChallengeEntity[];
}
xfyts7mz

xfyts7mz1#

因此,CountryEntityLevelEntity不知道它们的相关IdeaEntity[]是有道理的,这就是为什么在这两个实体中您与IdeaEntity没有关系。
这就引出了一个问题:你只是错过了连接本身吗?

export class IdeaEntity extends BaseEntity {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({nullable: true})
    @IsString()
    @IsOptional()
    @Length(7, 9)
    idea_title: string;

    @Column({type: 'enum', enum: LevesStatus, default: LevelsStatus.OPEN})
    levels_status: LevelsStatus;

    @Column({type: 'enum', enum: Payment, default: Payment.NOT_PAID })
    payment_status: Payment;

    @Column({nullable: true, default: null})
    video: string;

    @Column({nullable: true, default: null })
    price: number

    @ManyToOne(() => LevelsEntity, (level) => level.name, {eager: true})
    @JoinColumn({name: 'level_id'})
    level: LevelEntity;

    @ManyToOne(() => CountryEntity, (country) => country.country_name, {eager: true})
    @JoinColumn({name: 'country_id'})
    country: CountryEntity;
}

我使用了以下文档:https://orkhan.gitbook.io/typeorm/docs/eager-and-lazy-relations
下面是eager的一些陷阱:
仅当使用find* 方法时,即时关系才起作用。如果使用QueryBuilder,则会禁用即时关系,并且必须使用leftJoinAndSelect来加载关系。即时关系只能用于关系的一端,使用即时:不允许在关系的两端都为true。
另外,为了完整起见,下面是一个find*方法的示例:

const questionRepository = dataSource.getRepository(Question)

// questions will be loaded with its categories
const questions = await questionRepository.find()

相关问题