I'm using tsrynge for dependency injection in my nodejs, koa application but at the moment I'm facing an issue with a dependency. I'm receiving the following error when I try to use the createPatient method of the service in the controller:
Any idea on this?
Here is my code:
patient.routes.ts
import Router, { Middleware } from "@koa/router";
import { IRoutes } from "../domain/interfaces/routes/routes.interface";
import { container, inject, injectable } from "tsyringe";
import { PatientController } from "../controllers/patient.controller";
@injectable()
export class PatientRoutes implements IRoutes {
private router: Router;
constructor(
@inject(PatientController) private _patientController: PatientController,
) {
this.router = new Router({ prefix: "/patient" });
this.router.get("/", this._patientController.getPatients);
this.router.post("/", this._patientController.createPatient);
}
get routes(): Middleware {
return this.router.routes();
}
}
const patientRoutes = container.resolve(PatientRoutes);
export default patientRoutes.routes;
patient.controller.ts
import { Context } from "koa";
import { container, inject, injectable } from "tsyringe";
import { PatientService } from "../services/patient.service";
@injectable()
export class PatientController {
constructor(@inject(PatientService) private patientService: PatientService) { }
public getPatients(ctx: Context): void {
console.log(container.isRegistered(PatientService));
ctx.body = { message: "Get all patients" };
}
public async createPatient(ctx: Context): Promise<void> {
console.log(this.patientService?.createPatient());
ctx.body = { message: "hola" };
}
}
patient.service.ts
import { injectable } from "tsyringe";
@injectable()
export class PatientService {
constructor() { }
public async createPatient(): Promise<any> {
console.log("Service works!");
}
}
container register
import { container } from "tsyringe";
import { UnitOfWork } from "@mikro-orm/core";
import { ClinicHistoryController } from "../controllers/clinic-history.controller";
import { PatientController } from "../controllers/patient.controller";
import { PatientService } from "../services/patient.service";
import { PatientRoutes } from "../routes/patient.routes";
export const setDependencies = () => {
// ==============================================================
// ===================== Controllers ============================
container.register(ClinicHistoryController, {
useClass: ClinicHistoryController,
});
container.register(PatientController, {
useClass: PatientController,
});
container.register(PatientRoutes, {
useClass: PatientRoutes,
});
// ==============================================================
// ===================== Persistence ============================
container.register("IUnitOfWork", { useClass: UnitOfWork });
// ==============================================================
// ===================== Services ===============================
container.register(PatientService, {
useClass: PatientService,
});
// ==============================================================
};
I already try everything but don't get the solution, thank you very much for the help