NestJS 无法解析 AuthServices 的依赖关系

NestJS can't resolve dependencies of the AuthServices

之后,回到我认为已解决的老问题。原来,当我 "fix" 旧问题用 JWT 创建新问题时。

所以又编译不了:

Nest 无法解析 AuthService 的依赖项(?、RoleRepository、JwtService)。请确保索引 [0] 处的参数在 AppModule 上下文中可用。 +25ms

真的很奇怪,因为这种方式在我的另一个项目上工作,无法理解我错在哪里。这里是 auth.service.ts:

@Injectable()
export class AuthService {
    constructor(
        @InjectRepository(User) private readonly userRepo: Repository<User>,
        @InjectRepository(Role) private readonly rolesRepo: Repository<Role>,
        private readonly jwtService: JwtService,
    ) { }

它得到了角色 jwtService 但问题出在 User,路径是正确的。这里是 app.module.ts:

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule, AuthModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: configService.dbType as any,
        host: configService.dbHost,
        port: configService.dbPort,
        username: configService.dbUsername,
        password: configService.dbPassword,
        database: configService.dbName,
        entities: ['./src/data/entities/*.ts'],
      }),
    }),
  ],
  controllers: [AppController, AuthController],
  providers: [AuthService],
})
export class AppModule { }

控制器和提供程序出现相同的编译错误,无法理解哪里出了问题...

您可能缺少 TypeOrmModule.forFeature([User]) 导入。通常,所有实体都导入到专用功能模块中。如果您只有一个模块(即 AppModule),除了 forRoot 导入之外,您还需要将 forFeature 导入放在那里。

@Module({
  imports: [
    TypeOrmModule.forRootAsync({...}),
    TypeOrmModule.forFeature([User, Role]),
  ],

全局问题是我尝试添加 AuthService 和 AuthController 两次。所以我从 app.module.ts 中删除它们,然后从 auth.module.ts:

中导出 AuthService