问题描述
- user_profile.entity.ts
@Entity()
@Unique(['username'])
export class UserProfile {
/** Columns */
@PrimaryGeneratedColumn('uuid')
readonly id: string;
@Column('timestamptz')
@CreateDateColumn()
readonly created_at: Date;
@Column('timestamptz')
@UpdateDateColumn()
readonly updated_at: Date;
@Apiproperty()
@Column({ length: 255 })
@Isstring()
@Length(3,50)
username: string;
@Column()
post_count: number;
@ApiProperty({ readOnly: true })
@Column('uuid')
@IsUUID('4')
user_id: string;
@ApiProperty({ readOnly: true })
@Column({ default: 0,nullable: true })
@IsOptional()
exp?: number;
@ApiProperty({ readOnly: true })
@Column({ default: 1,nullable: true })
@IsOptional()
level?: number;
/** Relations */
@OnetoOne((type) => User,(user) => user.profile,{ onDelete: 'CASCADE' })
@JoinColumn({ name: 'user_id',referencedColumnName: 'id' })
user: User;
// ** Hooks
@AfterLoad()
updatePostCount() {
this.post_count = this.user.posts.length;
}
}
- user.entity.ts
@Entity()
@Unique(['email'])
@Check(`
COALESCE((provider = 'local')::integer,0)
+
COALESCE(LENGTH(social_id::text)::boolean::integer,0)
= 1
`)
export class User {
/** Columns */
@Apiproperty()
@PrimaryGeneratedColumn('uuid')
readonly id: string;
@Apiproperty()
@Column('timestamptz')
@CreateDateColumn()
readonly created_at: Date;
@Apiproperty()
@Column('timestamptz')
@UpdateDateColumn()
readonly updated_at: Date;
@Apiproperty()
@Column({ length: 255 })
@IsEmail()
email: string;
/** Relations */
@OnetoMany((type) => Post,(post) => post.author)
posts: Post[];
@Apiproperty()
@OnetoOne((type) => UserProfile,(userProfile) => userProfile.user,{
cascade: true,})
profile: UserProfile;
@AfterLoad()
asdfasdf() {
console.log('유저 entity :');
}
}
- post.entity.ts
@Entity()
export class Post {
/** Columns */
@PrimaryGeneratedColumn('uuid')
readonly id: string;
@Column('timestamptz')
@CreateDateColumn()
readonly created_at: Date;
@Column('timestamptz')
@UpdateDateColumn()
readonly updated_at: Date;
@Apiproperty()
@Column({ length: 255 })
@Isstring()
title: string;
@Apiproperty()
@Column('text')
@Isstring()
contents: string;
@ApiProperty({ readOnly: true })
@Column('uuid')
@IsUUID('4')
user_id: string;
/** Relations */
@ManyToOne((type) => User,(user) => user.posts,// eager: true,onDelete: 'CASCADE',})
@JoinColumn({ name: 'user_id',referencedColumnName: 'id' })
author: User;
}
- users.service.ts
async getMyUser(user_id: string) {
const test = await this.userProfileRepository
.createqueryBuilder('user_profile')
.leftJoinAndSelect('user_profile.user','user')
.leftJoinAndSelect('user.posts','posts')
.where('user.id = :id',{ id: user_id })
.getone();
return test;
}
async getUserProfile(user_id: string): Promise<UserProfileResponseDto> {
try {
const user = await this.userRepository
.createqueryBuilder('user')
.leftJoinAndSelect('user.profile','user_profile')
.leftJoinAndSelect('user.followers','followers')
.leftJoinAndSelect('user.following','following')
.leftJoinAndSelect('user.posts','posts')
.where('user.id = :id',{ id: user_id })
.getone();
if (!user) {
throw new Error();
}
return {
profile: user.profile,followers: user.followers.length,following: user.following.length,posts: user.posts.length,};
} catch (err) {
throw new BadRequestException('Invalid user_id');
}
}
在 user_profile
实体中,我使用了 @Afterload
,
当我使用 userPrifileRepository
(getMyUser) 时,输出很好,
但是如果我使用 userRepository
(getUserProfile),它就不会出现。 (this.user
或 this.user.posts.length
被打印为未定义。)
即使输出很好,也无法保存。
this.post_count = this.user.posts.length
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)