0

I'm developing an api server with App sync, node.js, typescript and serverless framework

I use inversify.js to make a di container. Here's the issue I am facing.

First of all, I define types.

  SAMPLE_USECASE: Symbol.for('SampleUseCase'),
  SAMPLE_REPOSITORY: Symbol.for('SampleRepository'),
  // repo: Symbol.for('SampleRepository'),
};

Second of all, I bind classes with interfaces.

container
  .bind<SampleRepository>(TYPES.SAMPLE_REPOSITORY)
  .to(SampleRepositoryImpl);
container.bind<SampleUseCase>(TYPES.SAMPLE_USECASE).to(SampleUseCaseImpl);

I expect these interfaces to be bound, and most of them are, but only one of them is not working. I got this error.

Cannot read properties of undefined (reading 'SAMPLE_REPOSITORY')"

The exported type object we first saw returns as undefined.

import { inject, injectable } from 'inversify';
import { container, TYPES } from 'src/registry/container';
import { info } from '@libs/logger';

import { Hello } from 'src/domains/hello/entity';
import { SampleRepository } from '../domains/hello/sampleRepository';

export interface SampleUseCase {
  getSample(id: number): Promise<Hello>;
}

@injectable()
export class SampleUseCaseImpl implements SampleUseCase {
   @inject(TYPES.SAMPLE_REPOSITORY) private sampleRepository:
    | SampleRepository
    | undefined;

  async getSample(id: number): Promise<Hello> {

    if (!this.sampleRepository) throw new Error();

    return await this.sampleRepository.getHello(id);
  }
}

What I made sure

  1. I passed a string into it and made sure this code works
  2. I checked the existence of the variable and it is not undefined elsewhere(the lamda handler) 3.I made sure the path is right.
  3. I made sure reflect-metadata is mentioned in my ts config file.
{
  "extends": "./tsconfig.paths.json",
  "compilerOptions": {
    "strict": true,
    "noImplicitReturns": true,
    "lib": ["ESNext"],
    "types": ["reflect-metadata", "node"],
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "ES2020",
    "outDir": "lib",
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "include": ["src/**/*.ts", "serverless.ts"],
  "exclude": [
    "node_modules/**/*",
    ".serverless/**/*",
    ".webpack/**/*",
    "_warmup/**/*",
    ".vscode/**/*"
  ],
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}

So it seems to me that the variable becomes undefined only when it is injected, but I don't understand why.

Can someone help me?

Here's my entire container file for your information.

import { Container } from 'inversify';
import { SampleRepositoryImpl } from '../repositories/sampleRepositoryImpl';
import { SampleUseCase, SampleUseCaseImpl } from 'src/useCases/sampleUseCase';
import { SampleRepository } from 'src/domains/hello/sampleRepository';

export const TYPES = {
  SAMPLE_USECASE: Symbol.for('SampleUseCase'),
  SAMPLE_REPOSITORY: Symbol.for('SampleRepository'),
  // repo: Symbol.for('SampleRepository'),
};



export const container = new Container();

container
  .bind<SampleRepository>(TYPES.SAMPLE_REPOSITORY)
  .to(SampleRepositoryImpl);
container.bind<SampleUseCase>(TYPES.SAMPLE_USECASE).to(SampleUseCaseImpl);
kappa
  • 107
  • 1
  • 12

0 Answers0