3

After first problem with JWT_MODULE_OPTION, back to old problem who I thought I was fixed. It turned out that when I "fix" old problem create the new with JWT.

So again can't compile:

Nest can't resolve dependencies of the AuthService (?, RoleRepository, JwtService). Please make sure that the argument at index [0] is available in the AppModule context. +25ms

It's really strange, because this way work on another my project and can't understand where I'm wrong. Here is the 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,
    ) { }

It get role and jwtService but the problem is with User, the path is correct. Here is 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 { }

Have the same compile error for controllers & providers & can't understand what is wrong...

Kim Kern
  • 54,283
  • 17
  • 197
  • 195

2 Answers2

3

You might be missing the TypeOrmModule.forFeature([User]) import. Typically, all entities are imported in dedicated feature modules. If you only have one module (i.e. AppModule) you need to put the forFeature import there in addition to the forRoot import.

@Module({
  imports: [
    TypeOrmModule.forRootAsync({...}),
    TypeOrmModule.forFeature([User, Role]),
  ],
Kim Kern
  • 54,283
  • 17
  • 197
  • 195
  • You are right, thanks. I have all entities in forFeature in CoreModule on my previous project. But when add User & Role in forFeature receive another compile error with AuthService: Nest can't resolve dependencies of the AuthService (UserRepository, RoleRepository, ?). Please make sure that the argument at index [2] is available in the AppModule context. +21ms I tried to add jwtService in forFeature but doesn't work... – Николай Матев Aug 13 '19 at 13:01
  • I added - inject: [ConfigService, JwtService], and think it's ok for now, but get new error... – Николай Матев Aug 13 '19 at 13:11
  • 1
    The `jwtService` has nothing to do with the `TypeormModule`. When you import the `JwtModule` it exposes the `JwtService`; but only in the module with the import. Have a look at this thread to learn how you can export a service to use it in other modules: https://stackoverflow.com/questions/51819504/inject-nestjs-service-from-another-module/51821523#51821523 – Kim Kern Aug 13 '19 at 13:11
  • Yes, when inject JwtService a receive this error --- Nest can't resolve dependencies of the TypeOrmModuleOptions (ConfigService, ?). Please make sure that the argument at index [1] is available in the TypeOrmCoreModule context. +25ms --- if I'll fix this, back to AuthService error... – Николай Матев Aug 13 '19 at 13:13
0

The global problem was that I try to add AuthService & AuthController twice. So I remove them from app.module.ts and just export AuthService from auth.module.ts: