繁体   English   中英

NestJS无法解析AuthServices的依赖项

[英]NestJS can't resolve dependencies of the AuthServices

遇到JWT_MODULE_OPTION的第一个问题后,回到我认为自己已解决的老问题。 原来,当我“解决”旧问题时,请使用JWT创建新问题。

所以再次无法编译:

Nest无法解析AuthService(?,RoleRepository,JwtService)的依赖项。 请确保在AppModule上下文中索引[0]处的参数可用。 + 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 ),你需要把forFeature进口有除了forRoot进口。

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

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

暂无
暂无

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

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