Reputation: 877
I have followed the guide on npm and github for inversify to configure dependency injection in my Typescript project.
I have a controller, a service and a router. The service is injected into the controller via constructor injection and the controller is pulled from the dependency injection container directly inside the router.
I get an error that 'Cannot read property 'listingService' of undefined'
.
It seems the controller is accessed but for some reason when I try to access the service i find it is undefined
.
Can anybody please enlighten me as to what the problem is?
The relevant skeleton code necessary to provide my working is as follows:
// ts.config.json
{
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": ["typings.d.ts", "server/**/*.ts"],
"exclude": ["node_modules"]
}
// TYPES.ts
const TYPES = {
ListingController: Symbol.for("ListingController"),
ListingService: Symbol.for("ListingService")
};
export { TYPES };
// inversify.config.ts
import "reflect-metadata";
import { Container } from "inversify";
import {TYPES} from "./Types";
import {ListingController} from "./interfaces/ListingController";
import {ListingControllerImpl} from "../controllers/ListingControllerImpl";
import {ListingService} from "./interfaces/ListingService";
import {ListingServiceImpl} from "../services/ListingServiceImpl";
const myContainer = new Container();
myContainer.bind<ListingController>(TYPES.ListingController).to(ListingControllerImpl);
myContainer.bind<ListingService>(TYPES.ListingService).to(ListingServiceImpl);
export { myContainer };
// router.ts
import * as express from 'express';
const app = express();
import {myContainer} from "../d.i./inversify.config";
import {TYPES} from "../d.i./Types";
import {ListingController} from "../d.i./interfaces/ListingController";
const listingController = myContainer.get<ListingController>(TYPES.ListingController);
app.post('/', listingController.create);
export default app;
export interface ListingController {
create(req: Request, res: Response): void;
}
export interface ListingService {
create(body: any, id: string): Promise<any>;
}
@injectable()
export class ListingControllerImpl implements ListingController {
public listingService: ListingService;
constructor()
constructor(@inject(TYPES.ListingService) listingService: ListingService) {
this.listingService = listingService;
}
public create(req: Request, res: Response): void {
this.listingService.create();
}
}
@injectable()
export class ListingServiceImpl implements ListingService {
constructor()
constructor() {
}
public all(uid: string, page: number, size): Promise<any> {
//
}
public byId(id: string, uid: string): Promise<any> {
//
}
public create(body: any, userId: string): Promise<any> {
// do something
}
}
Upvotes: 0
Views: 3060
Reputation: 877
Ok, so I have found the problem.
The issue was not actually anything to do with the dependency injection choice or implementation, but in fact was a slight misunderstanding I had of the closure property.
When I attempted to invoke the listingService's create method from the controller using this.listingService.create()
'this' was not pointing at an instantiation of the controller class.
By using instance function notation I gained access to the scope I was expecting and can now invoke the service method without any problems.
public create = (req: Request, res: Response, next: NextFunction): void => {
this.listingService.create();
});
I hope this can help somebody.
Upvotes: 3