Search code examples
node.jstypescriptnestjstypeorm

"Class constructor League cannot be invoked without 'new'" TypeORM QueryBuilder Error


In my small Nest.js project I have two entities: Player, League which are associated with ManyToMany association using PlayerLeagueStats table with some additional properties (OneToMany - (ManyToOne, ManyToOne) - OneToMany pattern). When I'm trying to assign a player to league using QueryBuilder, the query which adds a playerLeagueStats object to Player executes without problems, but the corresonding query which should add the same playerLeagueStats to League throws an error "Class constructor League cannot be invoked without 'new'". My entities:

@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 ....

Service method with queries:

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;
}

I load my entities using ormconfig file but I also tried to do this in AppModule forRoot() method according to Nest js docs and I still got the same error.

ormconfig.json
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "my_username",
"password": "any_password",
"database": "tennis_league",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true

}

and AppModule:

@Module({
  imports: [TypeOrmModule.forRoot(),
   LeagueModule,
   PlayerModule,
   PlayerLeagueStatsModule
  ],
  controllers: [AppController],
  providers: [
   {
     provide: APP_PIPE,
     useClass: ValidationPipe,
  },
   AppService
  ],
  })
export class AppModule {}

What could be reason of the error? I'm new in NodeJS and webdev in general. I'd be greatful for any help.


Solution

  • The error may be caused by TypeORM handling in the wrong way your "double" relation add operation, called on the same entity. It should work, however I think the best way to implement this many-to-many would be with just a save() operation, instead of three query:

    const playerLeagueStats= new PlayerLeagueStats();
    playerLeagueStats.player = player;
    playerLeagueStats.league = leagueToAssign;
    await this.playerLeagueStatsRepository.save(playerLeagueStats)
    

    This is the same thing you wrote, but it can be done in just one query since IDs are stored on PlayerLeagueStats entity table.