postgresql 有没有办法从NestJS框架中的模块文件访问数据库相关的东西?

ktca8awb  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(2)|浏览(123)

我试图从数据库中获取SMTP详细信息,而不是从.env文件中拾取它们。有人能建议我如何实现它吗?你可以在下面找到我的应用程序模块文件-

app.module.ts

@Module({
    imports: [
        TypeOrmModule.forRoot(configService.getTypeOrmConfig()),
        LanguageModule,
        UsersModule,
        MailsModule,
        MailerModule.forRoot({
            transport: {
                host: process.env.SMTP_HOST,
                port: process.env.SMTP_PORT,
                ignoreTLS: false,
                secure: false,
                auth: {
                    user: process.env.AUTH_USER,
                    pass: process.env.AUTH_PASS,
                },
            },
            defaults: {
                from: '[email protected]',
            },
            template: {
                dir: process.cwd() + '/email-templates',
                adapter: new HandlebarsAdapter(),
                options: {
                    strict: true,
                },
            },
        }),
        MiniCandidacyModule,
        ProgramsModule,
        AttachmentsModule,
        AdminConfigurationModule,
    ],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule {}

字符串
让我再提一件事,我已经尝试了nestjs动态模块的概念,但无法满足我的要求.我面临着一个问题,我的邮件模块.请找到应用程序模块文件的变化&下面的错误-

app.module.ts

@Module({
imports: [
    TypeOrmModule.forRoot(configService.getTypeOrmConfig()),
    LanguageModule,
    UsersModule,
    MailsModule,
    MailerModule.forRootAsync({
        imports: [AdminConfigurationModule],
        inject: [AdminConfigurationService],
        useFactory: async (adminConfigurationService: AdminConfigurationService) => {
            const options = await adminConfigurationService.getSmtpDetails()
            return options
        },
    }),
    MiniCandidacyModule,
    ProgramsModule,
    AttachmentsModule,
    AdminConfigurationModule,
   ],
   controllers: [AppController],
   providers: [AppService],
  })
  export class AppModule {}

错误

[Nest] 5520  -ERROR [ExceptionHandler] Nest can't resolve dependencies of the MAILER_OPTIONS (?). Please make sure that the argument AdminConfigurationService at index [0] is available in the MailerCoreModule context.   

Potential solutions:
- If AdminConfigurationService is a provider, is it part of the current MailerCoreModule?
- If AdminConfigurationService is exported from a separate @Module, is that module imported within MailerCoreModule?       
  @Module({
    imports: [ /* the Module containing AdminConfigurationService */ ]
  })

Error: Nest can't resolve dependencies of the MAILER_OPTIONS (?). Please make sure that the argument AdminConfigurationService at index [0] is available in the MailerCoreModule context.

Potential solutions:
- If AdminConfigurationService is a provider, is it part of the current MailerCoreModule?
- If AdminConfigurationService is exported from a separate @Module, is that module imported within MailerCoreModule?       
  @Module({
    imports: [ /* the Module containing AdminConfigurationService */ ]
  })

    at Injector.lookupComponentInParentModules (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\injector.js:193:19)
    at async Injector.resolveComponentInstance (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\injector.js:149:33)
    at async resolveParam (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\injector.js:103:38)
    at async Promise.all (index 0)
    at async Injector.resolveConstructorParams (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\injector.js:118:27)
    at async Injector.loadInstance (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\injector.js:47:9)
    at async Injector.loadProvider (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\injector.js:69:9)
    at async Promise.all (index 3)
    at async InstanceLoader.createInstancesOfProviders (E:\projects\hacep\backend\node_modules\@nestjs\core\injector\instance-loader.js:44:9)
    at async E:\projects\hacep\backend\node_modules\@nestjs\core\injector\instance-loader.js:29:13


提前感谢!

vc9ivgsu

vc9ivgsu1#

显然你的模块不是全局的,你需要在你的app模块中导入:

imports: [ConfigModule.forRoot({ isGlobal: true })],

字符串

envsm3lx

envsm3lx2#

显然你的模块不是全局的,你需要在你的应用模块中导入它:

imports: [ConfigModule.forRoot({ isGlobal: true })],

字符串
然后你需要导出它:

@Module({
 controllers: [ItemsController],
 providers: [ItemsService],
 exports: [ItemsService],
})

相关问题