【问题标题】:NestJS Error: "Nest can't resolve dependencies of the AuthService" even everything is wired up fineNestJS 错误:“Nest 无法解析 AuthService 的依赖关系”,即使一切正常
【发布时间】:2020-03-18 10:11:41
【问题描述】:

在我的 nestjs 应用程序中,我有一个 UserAuth 模块。 Auth 模块依赖于 User 模块来验证用户。

现在,即使我在模块级别正确连接了我的依赖项,我仍然收到依赖项解析错误(请参阅最后一部分)。

供参考,这是我的模块文件和相关服务。


src/user/user.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { UserService } from './services/user.service';
import { UserController } from './user.controller';
import { UserRepository } from './user.repository';

@Module({
  controllers: [UserController],
  imports: [TypeOrmModule.forFeature([UserRepository])],
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}

src/user/user.service.ts

/** imports... **/

@Injectable()
export class UserService {
  constructor(
    private readonly userRepository: UserRepository
  ) {
  }
  /** some codes **/
}

src/auth/auth.module.ts

import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';

import { UserModule } from '../user/user.module';
import { AuthController } from './auth.controller';
import { AuthService } from './services/auth.service';
import { JwtStrategy } from './strategies/jwt.strategy';

const jwtModule = JwtModule.register({
  secret: "SOME_SECRET",
  signOptions: { expiresIn: "1d" },
});

@Module({
  imports: [jwtModule, PassportModule, UserModule],
  providers: [AuthService, JwtStrategy],
  controllers: [AuthController],
  exports: [AuthService],
})
export class AuthModule {}

src/auth/auth.service.ts

import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

import { Status } from '../../shared';
import { UserEntity, UserService } from '../../user';

@Injectable()
export class AuthService {
  constructor(
    private readonly jwtService: JwtService,
    private readonly userService: UserService,
  ) {
  }
}

...作为参考,我的 user 模块文件夹中有一个桶文件。

src/user/index.ts

export * from './user.entity';
export * from './user.interface';
export * from './services/user.service';

app.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AuthModule } from './auth/auth.module';
import { CoreModule } from './core/core.module';
import { ConfigService } from './core/services/config.service';
import { SharedModule } from './shared/shared.module';
import { UserModule } from './user/user.module';


/**
 * ORM configuration
 */
const typeORM = TypeOrmModule.forRoot({
  /** typeorm configuration **/
});

@Module({
  imports: [
    typeORM,
    CoreModule,
    SharedModule,
    UserModule,
    AuthModule,
  ],
})
export class AppModule {}

如您所见,我将UserService 类导出到UserModule 中。 UserService 也用 @Injectable 装饰器装饰。最后,请注意我在AuthModule 的导入部分中导入了UserModule

但是当我尝试运行该应用程序时,我会遇到以下错误:

Nest can't resolve dependencies of the AuthService (JwtService, ?). Please make sure that the argument dependency at index [1] is available in the AuthModule context.

鉴于上述代码文件,嵌套应该没有理由无法在AuthService 中找到UserService。我错过了什么吗?

【问题讨论】:

  • 你能显示你的 aap.module.ts 文件吗?
  • 嗨@AntonSkybun,按照您的要求使用 app.module.ts 文件更新了 OP。
  • 奇怪,看起来很正常。你能在这里改变导入模块的顺序:[jwtModule, PassportModule, UserModule],先尝试设置USerModule
  • 我可以看到您的 UserModule 直接从 import { UserService } from './services/user.service'; 获取 UserService,而您的 AuthService 从桶文件 import { UserEntity, UserService } from '../../user'; 获取它。这不应该重要,但尝试将这些导入更改为相同的东西,看看是否会有所不同。
  • 嗨@nerdybeast,你部分正确!但问题实际上是通过桶文件导入实体。我使用的 typeorm 库在通过桶文件导入时似乎很难建立关系。我改变了那部分,它奏效了!但是,我的编码会遇到一致性问题,因为我必须明确定义我只为实体导入的文件的完整路径——我会找到一些替代方案。不过,感谢您的帮助。

标签: node.js typescript nestjs


【解决方案1】:

正如@nerdy beast 在评论中指出的那样,这个问题实际上与从桶文件中导入文件有关。就我而言,它与typeorm 相关——正如这里所讨论的https://github.com/typeorm/typeorm/issues/420#issuecomment-307219380

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 2020-08-12
    • 2021-01-28
    • 2021-09-06
    • 2020-08-06
    • 2021-07-28
    • 2019-01-21
    • 2022-01-17
    • 2018-11-21
    相关资源
    最近更新 更多