TypeORM懒加载字段返回undefined问题求助
解决TypeORM懒加载关联字段返回undefined的问题
问题根源
User实体关联字段配置错误:
一个用户对应多条评论,OneToMany的字段名应为复数reviews,类型是Promise<Review[]>,你写成了review: Promise<Review>,导致反向关联映射失败。Review实体ManyToOne关联指向错误:
@ManyToOne的第二个参数应指向User实体里的关联字段reviews,你写成了author => author.userId(直接指向主键),TypeORM无法识别正确的关联关系。构造函数参数类型错误:
创建Review实体时,构造函数别传Promise<Place>、Promise<User>,直接传实体实例即可,懒加载的Promise包装是TypeORM自动处理的,手动传Promise会打乱映射逻辑。Review里的OneToMany拼写错误:
reviewImage => reviewImage.reveiw里的reveiw写错成review,这会导致images关联也加载失败。
修正后的代码
user.entity.ts
import { BaseEntity } from 'src/global/base/base.entity'; import { Review } from 'src/review/entity/review.entity'; import { Column, DeleteDateColumn, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; import { Password } from './password'; @Entity('users') export class User extends BaseEntity { @PrimaryGeneratedColumn() userId: number; @Column() email: string; @Column(() => Password, { prefix: false }) password: Password; @Column() profileImage: string; @Column() username: string; @DeleteDateColumn() disabledAt: Date; @Column() refreshToken: string; // 修正字段名和类型,对应多条评论 @OneToMany(() => Review, review => review.author) reviews: Promise<Review[]>; constructor(email: string, password: Password, username: string) { super(); this.email = email; this.password = password; this.username = username; } public static createUser(email: string, password: Password, username: string): User { return new User(email, password, username); } }
review.entity.ts
import { BaseEntity } from 'src/global/base/base.entity'; import { Place } from 'src/place/entity/place.entity'; import { User } from 'src/user/entity/user.entity'; import { Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; import { ReviewImage } from './review-image.entity'; import { ReviewStatus } from './review-status'; @Entity('place_reviews') export class Review extends BaseEntity { @PrimaryGeneratedColumn() reviewId: number; @Column() visitedAt: Date; @Column() contents: string; @Column({ enum: ReviewStatus }) status: ReviewStatus; @Column({ type: 'float' }) rating: number; // 修正关联指向,指向User的reviews字段 @ManyToOne(() => User, author => author.reviews, { lazy: true }) @JoinColumn({ name: 'author', referencedColumnName: 'userId' }) author: Promise<User>; // 假设Place实体里的关联字段是reviews,需要对应修正Place实体 @ManyToOne(() => Place, place => place.reviews, { lazy: true }) @JoinColumn({ name: 'place_id', referencedColumnName: 'placeId' }) place: Promise<Place>; // 修正拼写错误:reveiw → review @OneToMany(() => ReviewImage, reviewImage => reviewImage.review, { lazy: true }) images: Promise<ReviewImage[]>; // 修正构造函数参数,直接传实体实例 constructor( visitedAt: Date, contents: string, rating: number, place: Place, author: User, images: ReviewImage[], ) { super(); this.visitedAt = visitedAt; this.contents = contents; this.rating = rating; this.place = place; this.author = author; this.images = images; } public static createReview( visitedAt: Date, contents: string, rating: number, place: Place, author: User, images: ReviewImage[], ): Review { return new Review(visitedAt, contents, rating, place, author, images); } }
额外检查点
- 确认
place_reviews表的author字段确实存储了users表的userId值,数据无错误。 - 同步修正Place实体的OneToMany关联,确保字段名和Review里的ManyToOne对应。
- 业务代码中从Repository获取的
review必须是TypeORM管理的实体,手动创建的实体懒加载不会生效。
内容的提问来源于stack exchange,提问作者Gihoon Eom
相关产品推荐
相关产品推荐

