typedi
typedi copied to clipboard
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?
Looks like you have circular dependencies here - (LoginService <-> SecurityService). See: https://docs.typestack.community/typedi/#problem-with-circular-references
I'm getting the same thing. I don't think it's a circular dependency problem @comprimator
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 seem to have the same / similar issue. there are no circular dependencies however i have services injecting services or values (simplified version below, not tested code):
@Service()
class A {
@Inject()
service1: Service1;
@Inject('value1')
value1: string;
}
@Service()
class Service1 {
@Inject('value1')
value1: string;
}
in this case value1
is undefined on class A
. I found that if i put the service inject after the value inject then it works. Feels like an unstable "fix" that could break randomly unless i'm missing something here.
I think this project is dead, there haven't been any commits or releases in a year.
I think this project is dead, there haven't been any commits or releases in a year.
any comment about that @pleerock @NoNameProvided ?
For the original issue, @comprimator was right, this is circular dependency and won't work. I am even surprised you say it runs and you get undefined. Assuming you target a recent ES version, it should not run and instead return the following error:
ReferenceError: Cannot access 'LoginService' before initialization
This is because the emited metadata will contain the ClassDefinition before it's declared:
/** Some other code */
/** LoginService is not yet defined here: */
__decorate([
(0, index_1.Inject)(),
__metadata("design:type", LoginService)
], SecurityService.prototype, "loginService", void 0);
SecurityService = __decorate([
(0, index_1.Service)()
], SecurityService);
exports.SecurityService = SecurityService;
let LoginService = class LoginService {
constructor(apiHelper, securityService) {
this.apiHelper = apiHelper;
this.securityService = securityService;
console.log(this.securityService); // undefined
console.log(index_1.Container.get(SecurityService)); // instance of SecurityService
}
};
LoginService = __decorate([
(0, index_1.Service)(),
__metadata("design:paramtypes", [ApiHelper,
SecurityService])
], LoginService);
exports.LoginService = LoginService;
I seem to have the same / similar issue. there are no circular dependencies however i have services injecting services or values (simplified version below, not tested code):
Please open a new issue with a working reproducible example so we don't need to guess what is the problem.
I think this project is dead, there haven't been any commits or releases in a year.
If a project doesn't receive updates it doesn't mean it's dead. Can we just move on from this thinking? Doesn't matter where is the last commit 1 year or 1 week ago, there is always someone who will call projects dead.
any comment about that @pleerock @NoNameProvided ?
pleerock is not involved with the typestack projects anymore as far as I can tell. I have not been too active recently as well as you can see.
For the original issue, @comprimator was right, this is circular dependency and won't work
agreed
I seem to have the same / similar issue. there are no circular dependencies however i have services injecting services or values (simplified version below, not tested code):
Please open a new issue with a working reproducible example so we don't need to guess what is the problem.
i took some time to create a reproducible example. the simplified version i posted above worked without an error. in our project we use routing-controllers and typeorm, along with their useContainer
methods and the Container from typeorm-typedi-extensions
. So i assume the issue could lie somewhere in that combination?
I think this project is dead, there haven't been any commits or releases in a year.
If a project doesn't receive updates it doesn't mean it's dead. Can we just move on from this thinking? Doesn't matter where is the last commit 1 year or 1 week ago, there is always someone who will call projects dead.
agreed. so i take this as a confirmation that this is not "dead". (i think it is still a valid question to politely ask in that case, since people need to make a responsible decision on what dependencies to include in their projects.)
agreed. so i take this as a confirmation that this is not "dead".
It's not and won't be, personally, I used all three (typedi, class-validator, class-transformer) libraries for work. I am not engaging with the community as much, because it takes a lot of time, and as said, I had no time to spare.
However, when issues arise, like a security issue in the last month, I have patched it in a timely manner. (Being the first from the affected libraries.)
I have some ideas how this situation can be improved, most of these "are we dead" questions come from the fact that there is no answer on issues. So I am thinking of enabling discussions where the community can figure out stuff, and keep issues strictly for bugs and features.
i took some time to create a reproducible example. the simplified version i posted above worked without an error. in our project we use routing-controllers and typeorm, along with their useContainer methods and the Container from typeorm-typedi-extensions. So i assume the issue could lie somewhere in that combination?
To reflect on your question, the following works for me without problems:
import 'reflect-metadata';
import { Container, Inject, Service } from './index';
Container.set({
id: 'value1',
value: 'constant-stuff'
})
@Service()
class Service1 {
@Inject('value1')
public value1!: string;
}
@Service()
class A {
@Inject()
public service1!: Service1;
@Inject('value1')
public value1!: string;
}
const instance = Container.get(A);
console.log(instance.service1);
console.log(instance.value1);
So I think the problem is not in TypeDI.
We're facing the same issue, same code worked a week ago. Now it gives undefined value from TypeDI.
@vs4vijay Could you provide a minimal reproduction?
@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.
Can we close this issue?
Original problem was the circular-dependency for injection, which can't logically work.
Also the last comment is very good about index.ts files. These can cause the same issues, because circular-imports might screw up the metadata for type-di.
Closing due to inactivity.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.