【问题标题】:Nest can't resolve dependencies of the MailerService (?)Nest 无法解析 MailerService 的依赖项(?)
【发布时间】:2020-09-03 04:27:57
【问题描述】:

我正在尝试配置this module

完整的错误信息如下:

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

我总是莫名其妙地收到这个错误,什么 MAILER_OPTIONS?这些选项在哪里?在这方面,文档中没有任何说明。完全不知道发生了什么。

这是我的 AppModule:

@Module({
  imports: [
    MailerModule.forRoot({
      defaults: {
        from: '"No Reply" <noreply@example.com>',
      },
      template: {
        dir: path.join(process.env.PWD, 'templates/pages'),
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
      options: {
        partials: {
          dir: path.join(process.env.PWD, 'templates/partials'),
          options: {
            strict: true,
          },
        },
      },
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    MailerService,
  ],
})
export class AppModule {}

有什么想法吗?

这是我正在使用的服务:

@Injectable()
export class AppService {
  constructor(
    private readonly mailerService: MailerService,
  ) {}

  public async sendEmail(): Promise<any> {
    const mailDetail: ISendMailOptions = {
      to: 'gutgesagt@yahoo.co.uk',
      from: 'noreply@nestjs.com',
      subject: 'testing Nest MailerModule',
      text: 'test test?!',
      html: '<b>Yahooooo</b>',
    };

    return this.mailerService.sendMail(mailDetail);
  }
}

【问题讨论】:

    标签: javascript node.js typescript dependency-injection nestjs


    【解决方案1】:

    您必须从AppModuleproviders 数组中删除导入的MailerService。只声明作为模块本身一部分的提供者;您将永远声明导入的提供程序(服务)。

      providers: [
        AppService,
      ],
    

    【讨论】:

    • 啊啊,明白了。谢谢!
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2021-01-28
    • 2021-11-24
    • 2019-01-21
    • 1970-01-01
    相关资源
    最近更新 更多