Contructor injection does noot work with classes
See original GitHub issueDescription
I have AuthRouter class depends on AuthController which is also depends on AuthService. I inject the dependencies in the constructor of each class but when I call the this.authService inside the AuthController, the injection does not work.
Minimal code-snippet showcasing the problem AuthRouter:
I create a class AuthRouter:
import { Router } from 'express'
import Container, { Service } from 'typedi'
import AuthenticationController from './index'
@Service()
class AuthenticationRouter {
constructor (private readonly authenticationController: AuthenticationController) {}
getRouter () {
const router = Router()
router.get('/auth/url', this.authenticationController.getAuthUrl)
return router
}
}
const authRouter = Container.get(AuthenticationRouter)
const routes = authRouter.getRouter()
export default routes
AuthController:
import { Request, Response } from 'express'
import { Service } from 'typedi'
import AuthenticationService from './authentication-service'
@Service()
class AuthenticationController {
constructor (private readonly authService: AuthenticationService) {}
async getAuthUrl (req: Request<{}, {}, {}, {redirect: string}>, res: Response) {
return res.redirect(this.authService.generateAuthenticationUrl(req.query.redirect))
}
}
export default AuthenticationController
Expected behavior
The dependency should be injected correctly by constructor.
Actual behavior
Dependency does not injected and I got an error:
(node:6064) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'authService' of undefined
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8
Top Results From Across the Web
Dependency injection in a class not working on a worker service
"The issue I am facing is where I say : var partService = new PartService(); It is telling me that I am missing...
Read more >3.4.1.1 Constructor-based dependency injection - Spring
Dependency injection (DI) is a process whereby objects define their dependencies, ... However, most Spring users do not work with these classes directly ......
Read more >Constructor Dependency Injection in Spring - Baeldung
This quick tutorial will explore a specific type of DI technique within Spring called Constructor-Based Dependency Injection, which simply put, ...
Read more >Why You Should Use Constructor Injection in Spring
Dependency injection is a common approach to implement loose coupling among the classes in an application. There are different ways of ...
Read more >Contexts and Dependency Injection - Quarkus
Quarkus DI solution (also called ArC) is based on the Contexts and Dependency Injection for Java 2.0 specification. However, it is not a...
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

this has nothing to do with typedi, is about javascript binding, see the error:
Cannot read property 'authService' of undefinedthis means that
thisis undefined, and that’s because when you pass thegetAuthUrlfunction torouter.getit’sthisis not theAuthenticationControllerinstance anymore. test this code to see what happens: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.