【问题标题】:Nest can't resolve dependencies to exported serviceNest 无法解析对导出服务的依赖关系
【发布时间】:2021-06-03 16:42:24
【问题描述】:

我正在尝试在 NestJS 中导出和导入服务。这看起来很简单,我认为它应该像这样工作,但我收到一个错误,说 Nest 无法解决依赖关系。

设置模块

这个模块有应该导入的服务,然后导出它。

@Module({
  imports: [
    MongooseModule.forFeature([{ name: Setting.name, schema: SettingSchema }]),
  ],
  providers: [SettingsService],
  exports: [SettingsService],
})
export class SettingsModule {}

MsgraphModule

这个模块应该通过模块导入服务,因为服务是在他们的服务中注入的。

@Module({
  imports: [SettingsModule],
  providers: [MsgraphService],
})
export class MsgraphModule {}

AppModule

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost/lead-import', {
      useCreateIndex: true,
    }),
    MsgraphModule,
    SettingsModule,
    ...
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

我在这里做错了什么?

【问题讨论】:

  • 你能提供SettingsService代码吗?

标签: javascript typescript module dependencies nestjs


【解决方案1】:

问题是我使用了 @Inject() 装饰器,它只用于自定义依赖注入。

@Injectable()
export class MsgraphService {
  private client: Client;
  private authenticator;

  constructor(@Inject() private settingsService: SettingsService) {
    this.init();
    this.authenticator = new MSGraphAuthenticator();
  }

  ...
}

所以删除 @Inject() 就成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 2019-02-24
    • 2019-06-29
    • 1970-01-01
    • 2022-01-17
    • 2018-11-21
    • 2020-03-12
    • 2020-07-03
    相关资源
    最近更新 更多