简体   繁体   English

NestJS 错误:“Nest 无法解析 AuthService 的依赖关系”,即使一切都连接正常

[英]NestJS Error: “Nest can't resolve dependencies of the AuthService” even everything is wired up fine

In my nestjs application, I have a User and Auth module.在我的 nestjs 应用程序中,我有一个UserAuth模块。 Auth module depends on the User module for validating users. Auth模块依赖于User模块来验证用户。

Now, even though I wired up my dependencies properly in the module level, I am getting the dependencies resolution error (See the last part).现在,即使我在模块级别正确连接了我的依赖项,我仍然收到依赖项解析错误(请参阅最后一部分)。

For reference, here are my module files and the relevant services.作为参考,这是我的模块文件和相关服务。


src/user/user.module.ts 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 src/user/user.service.ts

/** imports... **/

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

src/auth/auth.module.ts 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 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,
  ) {
  }
}

...and for reference, I have a barrel file in my user module folder. ...作为参考,我的user模块文件夹中有一个桶文件。

src/user/index.ts src/用户/index.ts

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

app.module.ts 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 {}

As you can see, I have my UserService class exported in the UserModule .如您所见,我在 UserModule 中导出了我的UserService UserModule UserService is also decorated with the @Injectable decorator. UserService也用@Injectable装饰器装饰。 Finally, notice that I imported the UserModule in my AuthModule 's import section.最后,请注意我在AuthModule的导入部分中导入了UserModule

But when I try to run the app, I am presented with this error:但是当我尝试运行该应用程序时,会出现以下错误:

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.

Given the above code files, there should be no reason for nest to not be able to locate the UserService within the AuthService .鉴于上述代码文件,nest 应该没有理由无法在AuthService中找到UserService Am I missing something?我错过了什么吗?

As @nerdy beast pointed out in the comment, the issue was actually related to importing file(s) from the barrel files.正如@nerdy beast 在评论中指出的那样,这个问题实际上与从桶文件中导入文件有关。 In my case, it is related to typeorm -- as discussed here https://github.com/typeorm/typeorm/issues/420#issuecomment-307219380 .就我而言,它与typeorm相关——如此处所述https://github.com/typeorm/typeorm/issues/420#issuecomment-307219380

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM