我创建了一个函数来在NestJS / TypeScript中创建新用户,
启动功能后,控制台会向我抛出该信息:
TypeError: Cannot read property 'create' of undefined
at Repository.create (/mnt/c/Users/.../Desktop/personal-data/node_modules/typeorm/repository/Repository.js:49:29)
at UserService.create (/mnt/c/Users/.../Desktop/personal-data/dist/user/user.service.js:60:40)
at AuthController.register (/mnt/c/Users/.../Desktop/personal-data/dist/auth/auth.controller.js:38:45)
有人可以告诉我我做错了什么吗?
我其余的代码:
export class CrudService<T extends ExtendedEntity>{
protected repository: Repository<T>;
constructor(
repository?: Repository<T>
){
if(repository){
this.repository = repository;
}
}
public async create(data: DeepPartial<T>): Promise<T> {
const entity: T = this.repository.create(data);
return entity.save();
}
}
import { CrudService } from 'src/helpers/service/crud-service';
@Injectable()
export class UserService extends CrudService<UserEntity>{
private logger = new Logger(UserService.name);
constructor(
protected readonly repository: Repository<UserEntity>,
) {
super();
}
public async create(data: DeepPartial<UserEntity>): Promise<UserEntity> {
const entity = this.repository.create(data);
await this.validate(entity);
entity.hashPassword();
if(!entity.createdAt){
entity.createdAt = new Date;
}
const user = await entity.save();
return user;
}
}
@Controller('api/auth')
@ApiTags('auth')
export class AuthController {
private logger = new Logger(AuthController.name);
constructor(
private readonly userService: UserService
){}
@Post('register')
@ApiBody({ required: true, type: UserEntityDto })
@ApiResponse({ status: 204, description: 'NO_CONTENT'})
public async register(@Body() data: DeepPartial<UserEntity>): Promise<void> {
const user = await this.userService.create(data);
this.logger.debug(`[register] User ${data.email} register`);
}
}
感谢您的帮助:) //////////////////////////////////////////////////// //////////////////////////////////////////////////// ///////////////////////////////////