【问题标题】:Nest cannot create the AuthModule instanceNest 无法创建 AuthModule 实例
【发布时间】:2022-01-11 22:27:00
【问题描述】:

尝试在用户模型和组之间建立链接时,出现错误:

我不明白为什么..

[Nest] 30312  - 06.12.2021, 20:53:42     LOG [NestFactory] Starting Nest application...
[Nest] 30312  - 06.12.2021, 20:53:42   ERROR [ExceptionHandler] Nest cannot create the AuthModule instance.
The module at index [1] of the AuthModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule]
Error: Nest cannot create the AuthModule instance.
The module at index [1] of the AuthModule "imports" array is undefined.

app.module.ts https://pastebin.com/T9EC05gS

users.module.ts https://pastebin.com/inS044Xk

user.model.ts https://pastebin.com/wpyUs2SW

groups.module.ts https://pastebin.com/Jvr2Fuk3

group.model.ts https://pastebin.com/nBvkaEUt

auth.module.ts https://pastebin.com/MYQ2dw6y

auth.guard.ts https://pastebin.com/xDpQ8vwE

auth.service.ts https://pastebin.com/G9TzN3XF

提前致谢

更新: 添加时出现错误

  @Expose()
  @HasMany(() => Group)
  groups: Group[];

到 user.model.ts

【问题讨论】:

  • 最好添加实际代码sn-ps而不是代码链接。虽然链接可以提供一些额外的上下文,但它们在问题的上下文中是不可搜索的,这使得问题更难找到
  • 我想以这种方式添加代码(直接进入问题),但网站说问题中有很多代码..

标签: typescript dependency-injection nestjs circular-dependency sequelize-typescript


【解决方案1】:

在您的auth.module.ts 文件中,而不是
import { UsersModule } from '../users'; 试试
import { UsersModule } from '../users/user.module';

我认为这些桶文件(index.ts 导出所有内容)不是一个好的模式,因为它在 NestJS 模块之间引入了循环导入。


1 auth.module.ts 导入 users/index.ts(我猜是导出 user.module.ts
2 groups.module.ts 导入 auth/index.ts(导出 @987654330 @ 我猜)
3 user.model.ts 导入 groups/index.ts

那么你最终会得到 1 导入 33 导入 1。这就是问题所在。

【讨论】:

  • 感谢您的帮助,它确实有效!说实话,通过 index.ts 的导出看起来不像是环依赖,这个不是很明显
猜你喜欢
  • 2021-01-15
  • 2020-01-16
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多