ylliX - Online Advertising Network
Vertex AI - Antrophic and Mistral models: Why does it require Imegen access?

NestJS Swagger – How to resolve TypeError: Cannot read properties of null (reading ‘name’)


I’m encountering the following error while setting up Swagger in my NestJS project:

/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:189
let schemaObjectName = trueMetadataType.name;
                                        ^
TypeError: Cannot read properties of null (reading 'name')
at SchemaObjectFactory.createNotBuiltInTypeReference (/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:189:49)
at SchemaObjectFactory.createSchemaMetadata (/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:297:25)
at SchemaObjectFactory.mergePropertyWithMetadata (/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:137:21)
...

This error occurs when generating the Swagger document in my main.ts file:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      forbidNonWhitelisted: true,
    }),
  );
  app.enableCors();

  const options = new DocumentBuilder()
    .setTitle('My API')
    .setDescription('My API description')
    .setVersion('1.0')
    .addBearerAuth()
    .build();

  const document = SwaggerModule.createDocument(app, options); // error here
  SwaggerModule.setup('swagger', app, document);

  const configService = app.get(ConfigService);
  const port = configService.get<number>('port');

  await app.listen(port || 8000);
}

I’m seeking advice on the cause of this error and how to resolve it. Specifically, I’d like to know:

  1. What are the common causes of this error?
  2. Are there any specific areas I should check in my DTO or entity classes?
  3. Are there any precautions I should take when using Swagger decorators?
  4. Can you suggest a step-by-step approach to troubleshoot and resolve this issue?

Versions I’m using:

  • NestJS: ^10.0.0
  • @nestjs/swagger: ^7.4.2

Thank you for your help!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *