Nest 无法解析 MailService 的依赖关系(?)

问题描述 投票:0回答:1

我创建了一个邮件模块,用于处理发送电子邮件,但设置后,当我尝试启动应用程序时,我不断收到错误消息:

Nest 无法解析 MailService 的依赖关系(?)。请确保索引 [0] 处的参数 Function 在 MailModule 上下文中可用。 可能的解决方案: - MailModule 是有效的 NestJS 模块吗? - 如果 Function 是提供者,它是当前 MailModule 的一部分吗? - 如果 Function 是从单独的 @Module 导出的,那么该模块是否在 MailModule 中导入? @Module({ import: [ /* 包含函数的模块 */ ] })

这是我的邮件模块:

import { Module } from '@nestjs/common';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { MailerModule } from '@nestjs-modules/mailer';
import { ConfigModule, ConfigService } from '@nestjs/config';

import { join } from 'node:path';
import { MailService } from './mail.service';
import { MailController } from './mail.controller';

@Module({
  imports: [
    ConfigModule.forRoot(),
    MailerModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        transport: {
          host: configService.get<string>('SMTP_HOST'),
          port: configService.get<number>('SMTP_PORT'),
          auth: {
            user: configService.get<string>('SMTP_USER'),
            pass: configService.get<string>('SMTP_PASSWORD'),
          },
        },
        defaults: {
          from: `"No Reply" <${configService.get<string>('SMTP_USER')}>`,
        },
        template: {
          dir: join(__dirname, 'templates'),
          adapter: new HandlebarsAdapter(),
          options: {
            strict: true,
          },
        },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [MailService],
  exports: [MailService],
  controllers: [MailController],
})
export class MailModule {}

这是邮件服务:

import { Injectable } from '@nestjs/common';
import type { MailerService } from '@nestjs-modules/mailer';

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

  async sendMail(to: string, subject: string, template: string, context: any) {
    await this.mailerService.sendMail({
      to,
      subject,
      template,
      context,
    });
  }
}

检查了文档,但似乎没有任何作用。

node.js typescript dependency-injection nestjs nodemailer
1个回答
0
投票

在第二个代码块中,您将导入“type { MailerService }”。尝试删除“类型”一词。

这里是我提到的例子。

© www.soinside.com 2019 - 2024. All rights reserved.