0

Screenshot of the error after trying the solution I get the following error

Cannot read properties of undefined (reading 'get')

Here is my code from the files.

@Injectable()
export class EVMService {
  public web3: Web3;

  @Inject(ConfigService)
  private readonly configService: ConfigService;

  constructor(@Optional() public chain: AvailableChains) {
    let jsonRPC: null | string = null;
    switch (chain) {
      case AvailableChains.BNB:
        jsonRPC = this.configService.get<string>('BNB_RPC_URL');
        break;
      case AvailableChains.ETH:
        jsonRPC = this.configService.get<string>('ETH_RPC_URL');
      default:
        break;
    }
    this.web3 = new Web3(jsonRPC);
  }
}
@Injectable()
export class BinanceService extends EVMService {
  constructor() {
    super(AvailableChains.BNB);
  }
}
@Global()
@Module({
  providers: [
    SharedService,
    EncryptionService,
    EVMService,
  ],
  exports: [SharedService],
})
export class SharedModule {}

I am expecting to the get the data from the config as it is declared globally.

1 Answers1

0

As you're not extending another provider in the EVMService, you should prefer injecting in the constructor instead of using a property, as stated in the docs.

The reason for this is because of how the metadata is read by NestJS, as stated in this answer.

You are extending the class in the BinanceService, so you're using it the correct.


So try this:

@Injectable()
export class EVMService {
  public web3: Web3;

  constructor(
    @Inject(ConfigService) 
    private readonly configService: ConfigService,
    
    @Optional() public chain: AvailableChains,
  ) {
    let jsonRPC: null | string = null;
    switch (chain) {
      case AvailableChains.BNB:
        jsonRPC = this.configService.get<string>('BNB_RPC_URL');
        break;
      case AvailableChains.ETH:
        jsonRPC = this.configService.get<string>('ETH_RPC_URL');
      default:
        break;
    }
    this.web3 = new Web3(jsonRPC);
  }
}
Bart Versluijs
  • 186
  • 1
  • 8
  • Still giving the same error. I have added a screenshot at the top – Hardil Singh Dec 09 '22 at 11:58
  • I can reproduce your problem, however I can't seem to find a solution for it. You are extending the service, so you're using it the correct way as the docs stated on the first try. But after an MVP, I get undefined injections as well. Maybe this is a bug in NestJS at the moment? – Bart Versluijs Dec 09 '22 at 12:16
  • Do you have any idea on how we may use configService in constructor it self because it is working with class methods but in constructor it does not load anything at all. – Hardil Singh Dec 09 '22 at 12:21