Reputation: 73
I am building a UserService class in my system, and declaring which should be a simple 'find' method, but I'm receiving the error as shown in the image bellow:
This is my service
method:
@Injectable()
export class UsersService {
...
async findByRA(academicRegister: string) {
return await this.userRepository.findOne({
where: { academicRegister: academicRegister },
});
}
...
}
This is my controller
:
@Controller('users')
export class UsersController {
...
@Get('ra/:ra')
findByRa(@Param('ra') ra: string) {
return this.usersService.findByRA(ra);
}
...
}
This is my User
entity, and if I change eager
to false
, the error does not happen, however I don't get the data from the child entities, which I need
@Entity()
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
@ApiProperty({ enum: UserRole, enumName: 'UserRole' })
userRole: UserRole;
@Column()
academicRegister: string;
@Column()
password: string;
@OneToOne(() => Coordinator, (coordinator) => coordinator.user, {
eager: true,
nullable: true,
cascade: ['insert', 'update', 'soft-remove', 'recover'],
})
coordinator?: Coordinator;
@OneToOne(() => Monitor, (monitor) => monitor.user, {
eager: true,
nullable: true,
cascade: ['insert', 'update', 'soft-remove', 'recover'],
})
monitor?: Monitor;
@CreateDateColumn()
creationDate: Date;
@DeleteDateColumn()
deletionDate?: Date;
@Column({ nullable: true })
deletedBy?: string;
}
These are the related columns in the child entities:
export class Coordinator {
...
@Column({ nullable: true, unique: true })
userId: string;
@OneToOne(() => User, (user) => user.coordinator, {
orphanedRowAction: 'nullify',
cascade: ['insert', 'update', 'soft-remove', 'recover'],
})
user: User;
...
}
export class Monitor {
...
@Column({ nullable: true, unique: true })
userId: string;
@OneToOne(() => User, (user) => user.monitor, {
orphanedRowAction: 'nullify',
cascade: ['insert', 'update', 'soft-remove', 'recover'],
})
user: User;
...
}
Upvotes: 0
Views: 337
Reputation: 239
Eager relations are loaded automatically each time you load entities from the database.
When You will be loading your Coordinator
or Monitor
entity you don't need to have manually call for your relations it will be done automatically.
You must be accessing your entities in wrong way.
Upvotes: 0