What is wrong in inheritance in Typescript?
i create this class:
export class ExtendedService<T extends ExtendedEntity> {
protected repository: Repository<T>;
constructor(
repository?: Repository<T>
){
this.repository = repository;
}
public async create(data: DeepPartial<T>): Promise<T> {
const entity: T = this.repository.create(data);
return entity.save();
}
}
and extends this class in this one:
@Injectable()
export class UserService extends ExtendedService<UserEntity> {
constructor(
protected readonly repository: Repository<UserEntity>
){
super();
}
public async register(data: DeepPartial<UserEntity>): Promise<UserEntity> {
const user = new UserEntity();
user.createdAt = new Date();
user.firstName = data.firstName;
user.lastName = data.lastName;
user.email = data.email;
user.role = UserRole.USER;
user.isDeleted = false;
const salt = await genSalt(10);
user.password = await hash(data.password, salt);
return await this.create(user);
}
}
but after launch this function, my console throw me this:
[Nest] 19980 - 2020-06-29 17:30:56 [ExceptionsHandler] Cannot read property 'create' of undefined +1378ms
TypeError: Cannot read property 'create' of undefined
at Repository.create (C:\Users\Piotruś\Desktop\auth\node_modules\typeorm\repository\Repository.js:49:29)
at UserService.create (C:\Users\Piotruś\Desktop\auth\dist\helpers\service\extended-service.js:27:40)
at UserService.register (C:\Users\Piotruś\Desktop\auth\dist\user\user.service.js:34:27)
at async UserController.register (C:\Users\Piotruś\Desktop\auth\dist\user\user.controller.js:25:9)
at async C:\Users\Piotruś\Desktop\auth\node_modules\@nestjs\core\router\router-execution-context.js:46:28
at async C:\Users\Piotruś\Desktop\auth\node_modules\@nestjs\core\router\router-proxy.js:9:17
so, the problem is in ts inheritance, can anybody tell me what is wrong with my inheritance?
createcall.const entity: T = this.repository.create(data);is where the issue is.this.repositoryisundefined.Repository, but you're not showing its code. Nothing to do with inheritance...Repositoryis atypeormclass