typedi: question: why some of injected services are undefined?

The problem: I have 3 services:

ApiHelper

@Service()
export class ApiHelper {
	// contains only methods, none of services are injected
}

LoginService

@Service()
export class SecurityService {
    @Inject()
    private loginService: LoginService;
}

And SecurityService

@Service()
export class LoginService {
    constructor(
        private apiHelper: ApiHelper,
        private securityService: SecurityService,
    ) {
        console.log(this.securityService); // undefined
        console.log(Container.get(SecurityService)); // instance of SecurityService
    }
}

And the problem is that securityService is undefined, while Container.get(SecurityService) works good.

What is wrong here?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 16 (5 by maintainers)

Most upvoted comments

We’re facing the same issue, same code worked a week ago. Now it gives undefined value from TypeDI.

@attilaorosz Looks like I had some circular dependency due to usage of index.ts… I imported directly without using index.ts, and it started working.

I’m getting the same thing and it’s not circular dependencies. It seems that the dependency tree is borked. If I debug at the constructor level, the order of instantiation is not correct.

I’m getting the same thing. I don’t think it’s a circular dependency problem @comprimator