Nest无法解析全局模块中服务的依赖项 [英] Nest can't resolve dependencies of the Service in Global Module
问题描述
我有一个名为 FirebaseModule
的全局模块,该模块可以导出 FirebaseService
,然后将此模块导入到 app.module.ts
中,然后尝试在 UsersService
中使用 FirebaseService
,它会因缺少依赖项问题而失败.
I have a global module called FirebaseModule
which exports a FirebaseService
, I import this module to the app.module.ts
and then I try to use the FirebaseService
inside a UsersService
and it fails with a missing dependency issue.
有趣的是,如果我删除了服务上的用法并仅在控制器上使用它,那么什么都不会失败,但是如果我想在服务提供者中使用它,它将开始失败.
Something interesting is that if I remove the usage on the service and use it only on the controller nothings fails but if I want to use it in the service provider, it starts failing.
我的代码:
app.module.ts
@Module({
imports: [
CustomLoggerModule,
ConfigurationModule,
TypeOrmModule.forRootAsync({
useExisting: ConfigurationService,
}),
FirebaseModule,
UsersModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
firebase.module.ts
@Global()
@Module({
imports: [],
providers: [FirebaseService],
exports: [FirebaseService],
})
export class FirebaseModule {}
users.module.ts
@Module({
imports: [CqrsModule, TypeOrmModule.forFeature([UsersRepository])],
controllers: [UsersController],
providers: [UsersService, ...CommandHandlers, ...EventHandlers],
exports: [UsersService, TypeOrmModule.forFeature([UsersRepository])],
})
export class UsersModule {}
users.controller.ts
@Controller('users')
@UseGuards(AuthGuard('firebase'))
export class UsersController {
constructor(
private readonly usersService: UsersService,
private readonly firebaseService: FirebaseService,
) {}
}
users.service.ts
@Injectable()
export class UsersService {
constructor(
private readonly firebaseService: FirebaseService,
private readonly repository: UsersRepository,
) {}
}
我收到以下错误:
Nest can't resolve dependencies of the UsersService (?, UsersRepository). Please make sure that the argument dependency at index [0] is available in the UsersModule context.
推荐答案
您很有可能在UserService中有一个对FirebaseService的循环文件引用.如果Nest能够解析类/提供者/注入令牌的名称,它会明确指出该令牌的含义,但是当它无法解析时,所有的结果就是 dependency
.从文档中获取更多信息
You have a circular file reference in your UserService to the FirebaseService most likely. If Nest is able to resolve the name of the class/provider/injection token, it will say what that token is explicitly, but when it is unable to, all the comes out is dependency
. Here's a little more information from the docs
这篇关于Nest无法解析全局模块中服务的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!