Inversify error in constructor of a class "Cannot read properties of undefined (reading 'Configuration')"

45 views Asked by At

I'm trying to use inversify DI container and following is how I'm doing it

export const Types = {
  IApi: Symbol.for("IAPI"),
  AxiosImpl: Symbol.for("AxiosImpl"),
  Executer: Symbol.for("Executer"),
  Query: Symbol.for("Query"),
  Configuration: Symbol.for("Configuration")
}

`  build: (c: Configuration) => {
    if (container.isBound(Types.Configuration)) {
      container.unbindAll()
    }

    container.bind<Configuration>(Types.Configuration).toConstantValue(c)
    container.bind<AxiosImpl>(Types.AxiosImpl).to(AxiosImpl)`

this is how the constructor is defined

`@injectable()
export class AxiosImpl implements IApi {
  private readonly _api: axios.AxiosInstance

  constructor(@inject(Types.Configuration) readonly configuration: Configuration) {
    const config: AxiosRequestConfig = {
      baseURL: configuration.baseURL,
      headers: {
        "Content-Type": configuration.contentType
      },
      timeout: configuration.timeOut,
      maxBodyLength: configuration.maxBodySize
    }
 // other code
}`

but it always give error at runtime

TypeError: Cannot read properties of undefined (reading 'Configuration')

      21 |   private readonly _api: axios.AxiosInstance
      22 |
    > 23 |   constructor(@inject(Types.Configuration) readonly configuration: Configuration) {
         |                             ^
      24 |     const config: AxiosRequestConfig = {

I read documentation but could not find the actual issue. Can anyone help me?

0

There are 0 answers