Search code examples
nestjsnestjs-typeorm

TypeOrmModule in Nest causes controller params to be undefined


In my Nest js app, This is the code of the app.controller:

@Controller('app')
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('/:id')
  getHello(@Param('id') id: string): string {
    console.log(id);
    return this.appService.getHello();
  }
}

It works perfect using the request: localhost:3000/app/1

When I add a functionality in my app.module to work with Postgres:

@Module({
  imports: [
    UsersModule,
    ScheduleModule.forRoot(),
    CacheModule.register({ isGlobal: true }),
    HttpModule,
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env.dev'],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'postgres',
        autoLoadEntities: true,
        synchronize: true,
        host: configService.get('DB_HOST'),
        port: configService.get('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_DATABASE'),
      }),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

The application is running, but I see that when I call to: localhost:3000/app/1 it gets to my controller with "undefined" in the param.

I will appreciate any help. Thanks!


Solution

  • Typeorm updated their version of reflect-metadata to 0.2.1 recently. Nest is compatible with it, but you need to update your application's version of it to match. Simply run your package manager's install command with the target as reflect-metadata@^0.2 and everything should be fine.

    npm install reflect-metadata@^0.2
    pnpm install reflect-metadata@^0.2
    yarn add reflect-metadata@^0.2