【问题标题】:NestJS can't resolve a service dependencyNestJS 无法解析服务依赖
【发布时间】:2020-05-27 05:27:42
【问题描述】:

我正在尝试将ContentsService 注入SlackModule,以便可以在我的 Slack 控制器中使用一些内容功能。

这是 app.module

@Module({
  imports: [
    ContentsModule,
    ZapierModule,
    SlackModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

这是我的内容模块:

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { ContentsController, ZapierController, SlackInteractiveController } from './contents.controller';
import { ContentsService } from './contents.service';
import { ContentSchema } from './content.model';

@Module({
  imports: [
    MongooseModule.forFeature([{ name: 'Content', schema: ContentSchema }]),
  ],
  controllers: [ContentsController, ZapierController, SlackInteractiveController],
  providers: [ContentsService],
  exports: [ContentsService],
})
export class ContentsModule {}

这是我的 Slack 模块:

import { Module } from '@nestjs/common';
import { SlackService } from './slack.service';
import { SlackController } from './slack.controller';
import { ContentsService } from '../contents/contents.service';

@Module({
  providers: [SlackService],
  controllers: [SlackController],
  imports: [ContentsService],
}) 
export class SlackModule {}

还有我的 Slack 控制器

import { ContentsService } from '../contents/contents.service'

@Controller('slackevents')
export class SlackController {  
  constructor(private contentsService: ContentsService) {}

但是当我运行这段代码时,我得到了这个错误:

[Nest] 75628 - 2020 年 5 月 22 日上午 7:08 [ExceptionHandler] Nest 无法解析 ContentsService (?) 的依赖项。请确保索引 [0] 处的参数在 ContentsService 上下文中可用。 +44 毫秒\

我做错了什么?

【问题讨论】:

    标签: node.js dependency-injection nestjs


    【解决方案1】:

    服务和其他提供者不属于imports 数组。只有模块类应该在那里。您的SlackModule 中应该有imports: [ContentsModule],您将可以访问ContentsService

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2019-12-19
      • 2022-01-02
      相关资源
      最近更新 更多