簡體   English   中英

NestJS 錯誤:“Nest 無法解析 AuthService 的依賴關系”,即使一切都連接正常

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

在我的 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/用戶/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 {}

如您所見,我在 UserModule 中導出了我的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.

鑒於上述代碼文件,nest 應該沒有理由無法在AuthService中找到UserService 我錯過了什么嗎?

正如@nerdy beast 在評論中指出的那樣,這個問題實際上與從桶文件中導入文件有關。 就我而言,它與typeorm相關——如此處所述https://github.com/typeorm/typeorm/issues/420#issuecomment-307219380

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM