New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Inject() doesn't work, Container.get() does #93
Comments
I have same issue, example below doesn't work, but works if I call Container.get instead: import 'reflect-metadata'
import assert from 'assert'
import {Container, Service, Inject} from 'typedi'
const key = 'authorization-token'
const data = 'RVT9rVjSVN'
Container.set(key, data)
@Service()
class UserRepository {
@Inject(key)
public authorizationToken: string;
}
const tmp = new UserRepository()
assert(tmp.authorizationToken === undefined)
assert(Container.get(key) === data)
console.log('asserts passed') Result:
My tsconfig have: "emitDecoratorMetadata": true,
"experimentalDecorators": true, Typedi version: 0.8.0 |
@georgyfarniev are you claiming about bug, but its actually wrong use from your side. Same happen for 80-90% issues usually opened on a github for open source projects. They are just questions or improper usage. In your case you shouldn't use |
Same maybe true for @lsmialowski request. He didn't actually decorated his |
@pleerock thank you for response. I will fix it in my code |
I have the same problem. I have all the services marked as Whenever I tried in both the constructor initialization version and the property declaration version to use Then I tried using None of that worked. But using So I have to agree that there's something up with the |
I'm also having issues with this. It seems to not work only in some cases but I haven't quite been able to track down exactly why it's happening. |
There is a high probability that you have a circular reference such that: import MyService from "./MyService";
console.log(MyService) // undefined
@Service()
export class MyController {
// Injection fails because metadata is added to the class as the module is loaded
// At the time the module loads in a circular dependency scenario the type was undefined
// Later because of the way the import is an object and the exported members are references
// on that module, the Type will appear. The it will always be undefined in the Reflect metadata.
@Inject()
private service: MyService
constructor() {
console.log(this.service) // undefined
console.log(MyService) // MyService is now defined after both modules are done loading
console.log(Container.get(MyService)) // MyService instance is created
}
} |
The core takeaway here:
This issue has multiple unrelated discussions now. Closing and lockin discussion. If anyone still experiences any difficulties, open a new issue. Thanks. |
Hi, I'm facing weird issue,
Container.get(MyServiceName); returns demanded service, yet class property decorated with @Inject() is undefined.
My tsconfig.json:
Service that I want to be injected
Service I want to inject
I do import reflect-metada at entry point of my application
I'm wondering if it may be related to fact, that that I'm using typedi not directly from my node_modules but from my dependency?
Application doesn't use Express framework neither routing controllers
The text was updated successfully, but these errors were encountered: