NestJS无法解析UsersModule的依赖项 [英] NestJS Cannot resolve dependencies of the UsersModule

查看:549
本文介绍了NestJS无法解析UsersModule的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NestJS无法解析UsersModule的依赖项.错误: Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.

NestJS Cannot resolve dependencies of the UsersModule. Error: Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.

app.module.ts:

app.module.ts:

@Module({
  imports: [
    ConfigModule,
    DatabaseModule,
    GraphQLModule,
    UsersModule,
  ],
  providers: [
    ErrorService,
  ],
  exports: [
    DatabaseModule,
    ErrorService,
  ],
})
export class AppModule implements NestModule {}

users.module.ts:

users.module.ts:

@Module({
    imports: [
        DatabaseModule,
        ErrorService,
    ],
    providers: [
        UsersService,
        ...usersProviders,
        UserResolver,
    ],
})
export class UsersModule {
    constructor(private readonly errorService: ErrorService) {}
}

问题是此ErrorService,但是例如数据库模块以类似的方式使用,并且可以正常工作而没有任何错误.我有点困惑)也许有人会帮忙.谢谢.

Problem is this ErrorService, but for instance Database module is used in similar way, and it works without any error. I'm little confused) Maybe somebody would help. Thank you.

推荐答案

ErrorService未正确注入UsersModule中.

它应该是:

  • UsersModuleproviders
  • 在由UsersModule
  • 组成的一个模块importexports
  • In the providers of UsersModule
  • In the exports of one module imported by UsersModule

否则,Nest将无法解决它.并将其添加到AppModuleexports中也不会使其全局可用.

Otherwise, Nest won't be able to resolve it. And adding it to the exports of AppModule doesn't make it globally available, either.

我可以看到三种解决方案:

I can see three solutions:

  • 1-将ErrorService添加到UsersModuleproviders中.但这似乎不是一个正确的方法,因为我认为/猜测您将在代码的许多其他部分重复使用此服务(对吗?).因此,更好:
  • 2-创建一个ErrorsModule,其中exports ErrorService,然后UsersModuleimport ErrorsModule并能够注入服务.
  • 1 - Adding ErrorService to the providers of UsersModule. But it doesn't look a proper way, as I think/guess that you will reuse this service in many other parts of the code (right?). Thus, better:
  • 2 - Create a ErrorsModule which exports the ErrorService, then UsersModule will import ErrorsModule and will be able to inject the service.

赞:

// errors.module.ts
@Module({
  providers: [ ErrorService ],
  exports: [ ErrorService ],
})
export class ErrorsModule {}

// users.module.ts
@Module({
  imports: [ErrorsModule],
})
export class UsersModule {
  constructor(private readonly errorService: ErrorService) {}
}

  • 3-我的最爱,如果您的服务应该被重用(LoggerService是一个更好的名字吗?如果我的猜测是正确的话),请使用一个@Global模块,您只需在您的计算机中import主模块(AppModule)
    • 3 - My favorite, if your service is supposed to be reused (would LoggerService be a better name? If my guess is correct), use a @Global module that you'll import only once in your main module (AppModule)
    • 示例:

      @Global()
      @Module({
        providers: [LoggerService],
        exports: [LoggerService],
      })
      export class LoggerModule {
        constructor(private readonly loggerService: LoggerService) { /* initialize logger, or whatever */ }
      }
      

      然后,您必须LoggerModule添加到AppModuleimports中,并且LoggerService将可以在任何地方注射,而无需重新声明.

      Then you must add LoggerModule to the imports of AppModule, and LoggerService will be injectable anywhere without having to re-declare it.

      这篇关于NestJS无法解析UsersModule的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆