我正在看这里的文件
https://docs.nestjs.com/techniques/authentication#jwt-functionality
为了获得更快的支持,我用这个问题创建了一个git存储库。
https://github.com/Sano123456/nestjs-jwt
node -v -> v10.15.2
npm -v -> 6.14.6
nest -v -> 7.4.1
第一个问题: in AuthModule如果我按照文档中的描述做并且只导入UserModule,它会返回UserModule和AuthModule之间循环依赖的错误
@Module({
imports:[
UsersModule,
PassportModule
],
providers: [AuthService],
controllers: [AuthController],
exports:[AuthService, LocalStrategy]
})
export class AuthModule {}
错误:
[ExceptionHandler] Nest cannot create the AuthModule instance.
The module at index [0] 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 [0] is of type "undefined". Check your import statements and the type of the module.
Scope [AppModule -> UsersModule] +6ms
可能解决方案在AuthModule导入数组中代替UserModule put forwardRef(() => UsersModule),这实际上消除了错误,但不确定这是否是正确的方法
第二个问题:,它说,即使它存在并在AuthModule中声明,也找不到LocalStrategy类
[ExceptionHandler] Nest cannot export a provider/module that is not a part of the currently processed module (AuthModule). Please verify whether the exported LocalStrategy is available in this particular context.Is LocalStrategy part of the relevant providers/imports within AuthModule?
可能性解决方案现在我没有任何解决方案,我只是删除它以了解问题所在
第三个问题:删除LocalStrategy后,
[ExceptionHandler] Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.
Potential solutions:
- If dependency is a provider, is it part of the current AuthModule?
- If dependency is exported from a separate @Module, is that module imported within AuthModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
+1ms
Error: Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.
有人解决了这个问题吗?
发布于 2020-08-25 08:57:10
您有一个循环依赖关系,因为您的UsersModule和AuthModule相互导入。因此,您有两个选项来修复此问题。
第一种方法是进行前向引用,这将允许同时构建两个模块,并在构建后传递所需的引用。这样做的方式如下:
@Module({
imports: [
forwardRef(() => UsersModule),
],
...
})
export class AuthModule {}
// And the same for your UsersModule
@Module({
imports: [
forwardRef(() => AuthModule),
],
})
export class UsersModule {}
第二种选择是从彼此之间删除受抚养人。这并不总是可能的,根据模块的名称来猜测,我认为这是不可能的。您不希望在用户模块之外让auth模块访问用户模块的数据库和服务等。但是,我将就模块继承给您一些建议。
nestjs中的模块是异步构建的。这意味着它们的结构必须像级联树一样,其中一个模块导入所有其他模块。线性的东西会像这样
AppModule <= CatModule <= PawsModule
另一个例子是双重进口。
<= CatModule
AppModule <= PawsModule
<= DogModule
在上面的例子中,paws模块被创建一次,并导入到cat和狗模块中。这意味着,在猫和狗模块之前,将先建立爪子模块。
因此,要解释您的错误,需要从线性代码的角度来考虑,以及模块导入树将如何共享相互导入的模块。因为它们相互导入,nest无法决定先创建哪一个,因此需要一个引用才能返回,因此函数。我一直认为容器的前向引用功能是:“嘿,拿着这个等一下”(这是一张写着“UsersModule”的纸),然后转过身来做一些手臂的波纹,然后转过身,用UsersModule代替那张纸!
第二个问题是,您从未为LocalStrategy
创建过提供程序。它不存在于AuthModule中,因此不能导入AuthService,也不能从AuthModule导出!
发布于 2020-08-26 11:21:31
**解决问题“不能解决依赖”**
对于第一个和第二个问题,最后的解决方案是在UserService中使用@Inject( => AuthService)
@Injectable()
export class UserService {
constructor(
@InjectRepository(User) private readonly UserRepository: Repository<User>,
private readonly config: ConfigService,
@Inject(forwardRef(() => AuthService)) //<---
private readonly authService: AuthService,
) {
}
在AuthService中也是一样的
@Injectable()
export class AuthService {
private client: any;
constructor(
@Inject(forwardRef(() => UserService))//<--- here
private readonly userService: UserService,
@InjectConfig() private readonly config,
) {
}
在AuthModule和UserModule 中,您仍然需要使用forwardRef
我以前从未使用过这个解决方案,也不需要它,但这解决了我的问题。
关于LocalStrategy,把它放在模块的提供者数组中
发布于 2022-06-29 15:59:14
对我来说,仅仅在这两个服务上使用forwardRef是不够的,就像在Sano的回答中那样;我还必须在两个循环依赖模块上使用forwardRef。
在这两个部门:
https://docs.nestjs.com/fundamentals/circular-dependency#forward-reference
import { forwardRef, Inject } from '@nestjs/common';
...
@Inject(forwardRef(() => UserService))`
在两个单元中:
https://docs.nestjs.com/fundamentals/circular-dependency#module-forward-reference
import { forwardRef } from '@nestjs/common';
...
imports: [
forwardRef(() => UsersModule),
],
https://stackoverflow.com/questions/63572923
复制相似问题