0
votes

I am working on a nestjs project using Kafka microservices (import from @nestjs/microservices).

for listening to a message, I am using the following code in main.ts:

  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        transport: Transport.KAFKA,
        options: {
          client: {
            brokers: configService.get('brokers'),
          },
        },
      }),
      inject: [ConfigService],
    },
  );

      await app.listen();

I am trying to let the nestjs read brokers from .env.

It can not work.

and I got error :

Argument of type '{ imports: (typeof ConfigModule)[]; useFactory: (configService: ConfigService) => Promise<{ transport: Transport; options: { client: { brokers: any; }; }; }>; inject: (typeof ConfigService)[]; }' is not assignable to parameter of type 'NestApplicationContextOptions & MicroserviceOptions'.
  Object literal may only specify known properties, and 'imports' does not exist in type 'NestApplicationContextOptions & MicroserviceOptions'.

if delete 'imports: [ConfigModule]', I have the following error:

Argument of type '{ useFactory: (configService: ConfigService) => Promise<{ transport: Transport; options: { client: { brokers: any; }; }; }>; inject: (typeof ConfigService)[]; }' is not assignable to parameter of type 'NestApplicationContextOptions & MicroserviceOptions'.
  Object literal may only specify known properties, and 'useFactory' does not exist in type 'NestApplicationContextOptions & MicroserviceOptions'.

Please help :)

using ClientsModule.registerAsync is the way to inject your ConfigService to retrieve env. vars. Edit your question to show us how you're using itMicael Levi
Hi , I have update the code.Erika