fix: TypeDI integration not working as expected on docs
See original GitHub issueDescription
Cannot use TypeDI as explained on the README for basic services.
Obviously, it is written that it only requires the @Service on service side (seems legit), but this actually doesn’t work without telling @Service before the controller as well.
Minimal code-snippet showcasing the problem
Controller snippet
import { Service } from 'typedi'
import { Get, JsonController, Param, State } from 'routing-controllers'
import { CollaboratorService } from '../services/CollaboratorService'
@JsonController('/service/:serviceId/collaborator')
@Service() // This shouldn't be required as described on the README, but it is actually required to run the app as expected
export class CollaboratorController {
constructor(private collaboratorService: CollaboratorService) { }
@Get('/')
async getCollaborators (@State('user') user: User, @Param('serviceId') serviceId: number) {
return await this.collaboratorService.getCollaborators(
user.id,
serviceId
)
}
Actual service snippet
import { Service } from 'typedi'
@Service()
export class CollaboratorService {
async getCollaborators (userId: number, serviceId: number) {
...
}
}
Expected behavior
I’ve imported everything and created the DI Container before the app, the Controller should be working without the @Service decorator declaration.
Actual behavior
It doesn’t, if I try to run the code without the @Service decorator on top of the Controller, I’ll get the following error:
{
"name": "ServiceNotFoundError",
"message": "Service with \"MaybeConstructable<CollaboratorService>\" identifier was not found in the container. Register it before usage via explicitly calling the \"Container.set\" function or using the \"@Service()\" decorator.",
"stack": "ServiceNotFoundError: Service with \"MaybeConstructable<CollaboratorService>\" identifier was not found in the container. Register it before usage via explicitly calling the \"Container.set\" function or using the \"@Service()\" decorator.\n at ContainerInstance.get (L:\\...\\node_modules\\typedi\\cjs\\container-instance.class.js:45:15)\n at Object.value (L:\\...\\node_modules\\typedi\\cjs\\decorators\\inject.decorator.js:31:42)\n at L:\\...\\node_modules\\typedi\\cjs\\container-instance.class.js:286:58\n at Array.forEach (<anonymous>)\n at ContainerInstance.applyPropertyHandlers (L:\\...\\node_modules\\typedi\\cjs\\container-instance.class.js:280:46)\n at ContainerInstance.getServiceValue (L:\\...\\node_modules\\typedi\\cjs\\container-instance.class.js:240:18)\n at ContainerInstance.get (L:\\...\\node_modules\\typedi\\cjs\\container-instance.class.js:29:25)\n at Function.get (L:\\...\\node_modules\\typedi\\cjs\\container.class.js:28:36)\n at Object.getFromContainer (L:\\...\\node_modules\\routing-controllers\\container.js:40:42)\n at ControllerMetadata.getInstance (L:\\...\\node_modules\\routing-controllers\\metadata\\ControllerMetadata.js:26:28)",
"normalizedIdentifier": "MaybeConstructable<CollaboratorService>"
}
I don’t know if this is an upstream issue or not.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Troubleshoot Google Docs, Sheets, Slides & Forms error ...
On your computer, open Google Drive. ... Settings. Turn on Offline setting. To work offline, open Google Docs, Sheets, or Slides. ... Hosted...
Read more >routing-controllers
fix : TypeDI integration not working as expected on docs $ 0. Created 1 year ago in typestack/routing-controllers with 6 comments. Description. Cannot...
Read more >typedi constructor injection return undefined
I imported 'reflect-metadata', in fact the decorators work fine if they are placed on properties and not in the constructor's parameters; typedi ......
Read more >Dependency Injection in JavaScript: Write Testable Code ...
The tools we use to manage these dependencies are different, but the problems are ultimately the same. A unit of code depends on...
Read more >Lucid Chart google sheets integration broken?
Recently I lost my google docs integration of lucidchart. ... links for lucidchart and google integration are broken, or indicate a problem.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I had to update ~60 classes to add
@Serviceeverywhere. This is just crazy. Couldn’t@Controllerdecorator automatically register controller/middleware classes with the provided DIC? I guess, this should be just a couple of lines of code in the library.Let’s keep this open for tracking. I think we may auto-register services, but we need to discuss this further.