Nest无法解析MailerService的依赖项(?)

I'm trying to configure 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);
  }
}
评论
  • 凹凸萌
    凹凸萌 回复

    You have to remove the imported MailerService from the providers array of your AppModule. Only declare providers that are part of the module itself; you would never declare an imported provider (service).

      providers: [
        AppService,
      ],