Cannot read properties of undefined when using inversifyJS inject

Viewed 134

I started a new project using express with typescript and i am trying to set up DI container with inversifyJS. Its suppose to be pretty simple use case with 1 route and controller My files so far:

controller\projects\projects.controller

import { inject, injectable } from "inversify";

import "reflect-metadata";
import { IProjectService } from "../../services/projects/interface/IProjectService";
import { TYPES } from "../../services/projects/types";

@injectable()
class ProjectController {
  private _processService: IProjectService;

  constructor(@inject(TYPES.IProjectService) projectServicee: IProjectService) {
    this._processService = projectServicee;
  }

  public getProjects(): string {
    return this._processService.getProject();
  }
}

export { ProjectController };

services\projects\imp\ProjectService.ts

import { injectable } from "inversify";
import "reflect-metadata";
import { IProjectService } from "../interface/IProjectService";

@injectable()
class ProjectServicee implements IProjectService {
  getProject(): string {
    return "Project A";
  }
}

@injectable()
class ProjectService implements IProjectService {
  getProject(): string {
    return "Project B";
  }
}

export { ProjectServicee, ProjectService };

services\projects\interface\IProjectService.ts

export interface IProjectService {
  getProject(): string;
}

routes\projects.ts Here i am asking the container for instance of the ProjectController and getting it as expected, But the private instance of IProjectService within it is undefined at runtime

import { myContainer } from "../../inversify.config";
import { ProjectController } from "../controllers/projects/projects.controller";
import { CONTROLLERS_TYPES } from "../controllers/types";

import * as express from "express";
const router = express.Router();

const pc = myContainer.get<ProjectController>(
  CONTROLLERS_TYPES.ProjectController
);

router.get("/", pc.getProjects);

export default router;

root\inversify.config.ts

import { Container } from "inversify";
import { TYPES } from "./src/services/projects/types";
import { IProjectService } from "./src/services/projects/interface/IProjectService";
import { ProjectService } from "./src/services/projects/imp/ProjectService";
import { CONTROLLERS_TYPES } from "./src/controllers/types";
import { ProjectController } from "./src/controllers/projects/projects.controller";
const myContainer = new Container();

myContainer
  .bind<ProjectController>(CONTROLLERS_TYPES.ProjectController)
  .to(ProjectController);
myContainer.bind<IProjectService>(TYPES.IProjectService).to(ProjectService);

export { myContainer };

controllers\types.ts

const CONTROLLERS_TYPES = {
  ProjectController: Symbol.for("ProjectController"),
};

export { CONTROLLERS_TYPES };

services\projects\types.ts

const TYPES = {
  IProjectService: Symbol.for("IProjectService"),
};

export { TYPES };

Finally my main:

import projectRoutes from "./routes/projects";

import express from "express";
const app = express();

app.get("/", function (req, res) {
  res.send("Hello World");
});

app.use("/api/projects", projectRoutes);

console.log("Listening on port 3000");
app.listen(3000);

Now, When trying to invoke the get method of the controller i get error: Cannot read properties of undefined (reading '_processService')

which means the initialization of the _processService in the controller did not worked, What am i missing?

0 Answers
Related