简体   繁体   中英

How can I in NestJS extend a class from another class?

I'm trying to extend a class service from another class service in NestJS and using DI to load it in a third service. I get the nest error:

 Error: Nest can't resolve dependencies of the AlgorithmsService (?, SocketService). Please make sure that the argument Object at index [0] is available in the AlgorithmsModule context.

Potential solutions:
- If Object is a provider, is it part of the current AlgorithmsModule?
- If Object is exported from a separate @Module, is that module imported within AlgorithmsModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })

So If I check the algorithm.service.ts


@Injectable()
export class AlgorithmsService {

    constructor(public coinMakerService: CoinMakerService,
        public socketService: SocketService) { }
}     

In app.module.ts

@Module({
  imports: [AlgorithmsModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

So the algorithm.module.ts

@Module({
    imports: [],
    exports: [],
    controllers: [AlgorithmsController],
    providers: [
        AlgorithmsService,
        CoinMakerService,
        CryptoService,
        SocketService
    ]
})
export class AlgorithmsModule { }

And finally the last classes that I prettend tyo extends:

@Injectable()
export class CryptoService {

    constructor() { }

}

@Injectable()
export class CoinMakerService extends CryptoService {

    constructor() {
        super()
    }
}

I dont know If I need to declare anything else. I tried to move all service to a shared module and Import it from top level app.module.ts but I get the same errors.

Since AlgorithmsService leaves in another module, you need to expose the providers in AlgorithmsModule that you can use outside of it

@Module({
    imports: [],
    exports: [CoinMakerService], // I'm not sure if you need to add CryptoService as well
    controllers: [AlgorithmsController],
    providers: [
        AlgorithmsService,
        CoinMakerService,
        CryptoService,
        SocketService
    ]
})
export class AlgorithmsModule { }

please read the docs https://docs.nestjs.com/modules

I've tried those exports too and they didn't work. In fact, SocketService does not give an error... And If I remove the extend CryptoService or public coinMakerService: CoinMakerService in constructor it works... I don't know if what really fails is that I need to indicate in some way the provide or some kind of special injector because nothing I test works. Always the same errors...

@Module({
    imports: [],
    exports: [
        CoinMakerService,
        CryptoService
    ],
    controllers: [AlgorithmsController],
    providers: [
        AlgorithmsService,
        CoinMakerService,
        CryptoService,
        SocketService
    ]
})
export class AlgorithmsModule { }
Nest can't resolve dependencies of the AlgorithmsService (?, SocketService). Please make sure that the argument Object at index [0] is available in the AlgorithmsModule context.

Potential solutions:
- If Object is a provider, is it part of the current AlgorithmsModule?
- If Object is exported from a separate @Module, is that module imported within AlgorithmsModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })
 Error: Nest can't resolve dependencies of the AlgorithmsService (?, SocketService). Please make sure that the argument Object at index [0] is available in the AlgorithmsModule context.

Solved. The problem was on paths in tsconfig.json It seems that NestJS is not very compatible wth this feature that I use a lot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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