Nest JS 依赖项

问题描述 投票:0回答:1

我正在尝试在 UserService 类上使用 UserRepository 类,并从 UserModule 中导出 UserService,以便我可以在 AuthService 类上使用该类,但我收到一些提及 UserRepository 的错误...

用户模块

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

用户存储库

@Injectable()
export class UserRepository {
  constructor(
    @InjectRepository(User) private readonly _userRepository: Repository<User>,
  ) {}}

用户服务

@Injectable()
export class UserService {
  constructor(
    private readonly _userRepository: UserRepository,
    private readonly _profileService: ProfileService,
    private readonly _cartService: CartService,
  ) {}}

认证服务

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(User)
    private readonly _userService: UserService,
    private readonly _jwtService: JwtService,
  ) {}}

认证模块

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.register({
      secret: '********',
      signOptions: {
        expiresIn: '6h',
      },
    }),
    UserModule,
  ],
  controllers: [AuthController],
  providers: [AuthService, JwtStrategy],
  exports: [JwtStrategy, PassportModule],
})
export class AuthModule {}

我遇到错误

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

Potential solutions:
- Is AuthModule a valid NestJS module?
- If "UserRepository" is a provider, is it part of the current AuthModule?
- If "UserRepository" is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing "UserRepository" */ ]
  })

我创建了一个用于数据库交互的类:UserRepository 类,一个用于业务逻辑的类:UserService,我将在 AuthService 上使用该服务

typescript dependency-injection nestjs backend nestjs-typeorm
1个回答
0
投票

@jay-mcdoniel 解决了它...谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.