索引 [1] 处的模块的类型为 "undefined"。检查您的导入语句和模块的类型

The module at index [1] is of type "undefined". Check your import statements and the type of the module

我遇到了 NestJs 中循环依赖的问题。错误:

Scope [AppModule -> PlanModule -> TaskModule] +17ms
Error: Nest cannot create the UserModule instance.
The module at index [1] of the UserModule "imports" array is undefined.

我尝试了什么:

@Inject(forwardRef(() => TaskService))
private readonly tasksService: TaskService
onModuleInit() {
  this.userService = this.moduleRef.get(UserService);
}
import { TaskModule } from '../task/task.module';

user.module

@Module({
  providers: [UserService, UserResolver],
  exports: [UserService],
  imports: [JournalModule, PlanModule, TaskModule],
})
export class UserModule {}

task.module

@Module({
  providers: [TaskService, TaskResolver],
  exports: [TaskService],
  imports: [UserModule],
})
export class TaskModule {}

plan.module

@Module({
  providers: [PlanService, PlanResolver, GoalResolver],
  imports: [TaskModule, ProgramModule],
  exports: [PlanService],
})
export class PlanModule {}

您的 UserModule 导入 TaskModuleTaskModule 导入 UserModule,因此您需要为这些模块导入设置前向引用 连同 服务注入。它看起来像这样:

user.module.ts

@Module({
  providers: [UserService, UserResolver],
  exports: [UserService],
  imports: [JournalModule, PlanModule, forwardref(() => TaskModule)],
})
export class UserModule {}

task.module.ts

@Module({
  providers: [TaskService, TaskResolver],
  exports: [TaskService],
  imports: [forwardRef(() => UserModule)],
})
export class TaskModule {}