In my setup, there are three classes that interact with each other. I am utilizing inversify
for handling dependency injection. However, I encountered an issue when I injected the class MessageBroker
into the derived class Repository
and found that the MessageBroker
was undefined:
import 'reflect-metadata';
import { injectable, inject, Container, unmanaged } from 'inversify';
const container = new Container();
const registerProviders = (...providers: any[]) =>
providers.forEach(provider => container.bind(provider.name).to(provider));
const getProvider = (provider): any => container.get(provider.name);
@injectable()
export class MessageBroker {
start = () => console.log('init message broker');
}
@injectable()
export abstract class Repository {
@inject(MessageBroker.name) private mb: MessageBroker;
constructor(@unmanaged() protected readonly user: any) {}
// this.mb is undefined
initialize = () => this.mb.start();
}
@injectable()
export class UserRepository extends Repository {
constructor() {
super({ user: 'some object' });
this.initialize();
}
}
registerProviders(UserRepository, Repository, MessageBroker);
const repo: UserRepository = getProvider(UserRepository);
To test it yourself, you can check out the sample code on GitHub: https://github.com/flolude/stackoverflow-inversify-injected-service-undefined
Upon running the script, I encountered the following error message:
/project/index.ts:22
initialize = () => this.mb.start();
^
TypeError: Cannot read property 'start' of undefined
at UserRepository.Repository.initialize (/project/index.ts:22:30)
at new UserRepository (/project/index.ts:29:10)
at _createInstance (/project/node_modules/inversify/lib/resolution/instantiation.js:21:12)
at Object.resolveInstance (/project/node_modules/inversify/lib/resolution/instantiation.js:41:18)
at /project/node_modules/inversify/lib/resolution/resolver.js:72:42
at Object.resolve (/project/node_modules/inversify/lib/resolution/resolver.js:96:12)
at /project/node_modules/inversify/lib/container/container.js:319:37
at Container._get (/project/node_modules/inversify/lib/container/container.js:310:44)
at Container.get (/project/node_modules/inversify/lib/container/container.js:230:21)
at getProvider (/project/index.ts:9:50)
P.S. The same error persisted even after compiling the code to Javascript