В моем небольшом проекте Nest. js у меня есть две сущности: Player, League, которые связаны с ассоциацией ManyToMany с помощью таблицы PlayerLeagueStats с некоторыми дополнительными свойствами (OneToMany - (ManyToOne, ManyToOne) - шаблон OneToMany). Когда я пытаюсь назначить игрока в лигу с помощью QueryBuilder, запрос, который добавляет объект playerLeagueStats к Player, выполняется без проблем, но соответствующий запрос, который должен добавить тот же playerLeagueStats к League, выдает ошибку «Конструктор класса League не может быть вызван без "новый». Мои сущности:
@Entity()
export class League {
@OneToMany(type => PlayerLeagueStats, playerLeagueStats => playerLeagueStats.league)
playerLeagueStats: PlayerLeagueStats[];
//other properties..
@Entity()
export class Player {
@OneToMany(type => PlayerLeagueStats, playerLeagueStats => playerLeagueStats.player)
playerLeagueStats: PlayerLeagueStats[];
//other properties ....
@Entity()
export class PlayerLeagueStats {
@PrimaryGeneratedColumn()
Id!: number;
@ManyToOne(type => Player, player => player.playerLeagueStats)
player: Player;
@ManyToOne(type => League, league => league.playerLeagueStats)
league: League;
//other additional properties ....
Метод обслуживания с запросами:
async assignPlayerToLeague(leagueId: number, playerId: number): Promise<League>{
const foundPlayer = await this.playerRepository.findOne(playerId)
const leagueToAssign = await this.LeagueRepository.findOne(leagueId);
const playerLeagueStats= new PlayerLeagueStats();
await this.playerLeagueStatsRepository.save(playerLeagueStats)
await this.playerRepository
.createQueryBuilder()
.relation(Player, "playerLeagueStats")
.of(foundPlayer)
.add(playerLeagueStats);
await this.LeagueRepository
.createQueryBuilder()
.relation(League, "playerLeagueStats")
.of(leagueToAssign)
.add(playerLeagueStats);
return leagueToAssign;
}
Я загружаю свои сущности с помощью файла ormconfig, но я также пытался сделать это в AppModule для метода Root () согласно в гнездо js документов, и я все еще получил ту же ошибку.
ormconfig.json
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "my_username",
"password": "any_password",
"database": "tennis_league",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true
}
и AppModule:
@Module({
imports: [TypeOrmModule.forRoot(),
LeagueModule,
PlayerModule,
PlayerLeagueStatsModule
],
controllers: [AppController],
providers: [
{
provide: APP_PIPE,
useClass: ValidationPipe,
},
AppService
],
})
export class AppModule {}
Что может быть причиной ошибки? Я новичок в NodeJS и webdev в целом. Я был бы благодарен за любую помощь.