Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example issue with configService? #81

Open
crea-vis-art opened this issue Jun 7, 2024 · 2 comments
Open

Example issue with configService? #81

crea-vis-art opened this issue Jun 7, 2024 · 2 comments

Comments

@crea-vis-art
Copy link

Hi,
I am new at NestJs.
Is this example correct? I have no Idea how to get the "configService" as described in the example.
I only can find examples with use of "useFactory".

`import { RMQModule } from 'nestjs-rmq';

@module({
imports: [
RMQModule.forRoot({
exchangeName: configService.get('AMQP_EXCHANGE'),
connections: [
{
login: configService.get('AMQP_LOGIN'),
password: configService.get('AMQP_PASSWORD'),
host: configService.get('AMQP_HOST'),
},
],
}),
],
})
export class AppModule {}`

Sorry but don't get the code formating in the github editor

@DIY0R
Copy link

DIY0R commented Jun 9, 2024

No, you can't write like that. If you want to declare dynamic, you can use forRootAsync and useFactory

@kevalin
Copy link
Contributor

kevalin commented Jun 14, 2024

No, you can't write like that. If you want to declare dynamic, you can use forRootAsync and useFactory

exactly!

This is a demo, and hope it can help you. @crea-vis-art
first, you need to create a .env file at your project root directory. Sure, you need to set your actual value.

# .env
AMQP_EXCHANGE=demo
AMQP_LOGIN=user
AMQP_PASSWORD=bitnami
AMQP_HOST=rabbitmq.orb.local
// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RMQModule } from 'nestjs-rmq';
import { ConfigService, ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    RMQModule.forRootAsync({
      imports: [ConfigModule.forRoot()],
      useFactory: (configService: ConfigService) => ({
        exchangeName: configService.get<string>('AMQP_EXCHANGE'),
        serviceName: 'demo',
        connections: [
          {
            login: configService.get<string>('AMQP_LOGIN'),
            password: configService.get<string>('AMQP_PASSWORD'),
            host: configService.get<string>('AMQP_HOST'),
          },
        ],
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants