General Note on my tech-stack: I have an application composed of Node.js - Express- Inversifyjs - Typescript
I have a modular structure on my express-routes on seperate files. Now when I try to inject my database-connector class on one of the routes classes, I get the error: TypeError: Cannot read property 'mongoDBClient' of undefined
Expected Behavior
My expected behaviour should be to normally inject a class into other classes, if i take into consideration that my inversify-config is setted properly.
Current Behavior
Inversify does not inject my class properly. Node detects injected class as undefined.
@injectable()
export default abstract class AbstractRoutes {
abstract ROUTE_PARAMS: string;
public router = express.Router();
abstract createEndpoints(): void;
public getRoutes() {
this.createEndpoints();
return this.router;
}
}
one of the route files
Error is thrown here
@injectable()
export default class AuthRoutes extends AbstractRoutes {
public ROUTE_PARAMS: string = '/auth';
constructor(@inject(TYPES.MONGO_DB_CLIENT) public mongoDBClient: MongoDBClient,
@inject(TYPES.EMAIL_CREATOR) public emailCreator: EmailCreator) {
super();
console.log(mongoDBClient); // <== is defined
console.log(mongoDBClient.connectionManager); // <== some property is undefined
}
public async checkLoggedIn(request: any, response: any) {
const sessionId = request.cookies.sid;
const uid = request.session?.user?.uid;
console.log(this.mongoDBClient); // <== crashes here
response.status(200).send({foo: 'bar'})
}
Context
The error is thrown in my route file in the checkdLogggedIn method. Normally i should be able to import the mongoDBClient class. Be aware that i import the mongoDBClient also in the parent Express class.
Your Environment
Versions:
Node: 12.13.0
Inversifyjs: 5.0.1
Reflect-metadata: 0.1.13
Typescript: 3.5.3
Stack trace
(node:7480) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'mongoDBClient' of undefined
at /home/marco/WebstormProjects/help-educate/backend/src/modules/server/routes/auth-routes.ts:24:26
at step (/home/marco/WebstormProjects/help-educate/backend/src/modules/server/routes/auth-routes.ts:58:23)
at Object.next (/home/marco/WebstormProjects/help-educate/backend/src/modules/server/routes/auth-routes.ts:39:53)
at /home/marco/WebstormProjects/help-educate/backend/src/modules/server/routes/auth-routes.ts:33:71
at new Promise (<anonymous>)
at __awaiter (/home/marco/WebstormProjects/help-educate/backend/src/modules/server/routes/auth-routes.ts:29:12)
at AuthRoutes.checkLoggedIn (/home/marco/WebstormProjects/help-educate/backend/src/modules/server/routes/auth-routes.ts:86:16)
at Layer.handle [as handle_request] (/home/marco/WebstormProjects/help-educate/backend/node_modules/express/lib/router/layer.js:95:5)
at next (/home/marco/WebstormProjects/help-educate/backend/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/marco/WebstormProjects/help-educate/backend/node_modules/express/lib/router/route.js:112:3)
(node:7480) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7480) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
General Note on my tech-stack: I have an application composed of Node.js - Express- Inversifyjs - Typescript
I have a modular structure on my express-routes on seperate files. Now when I try to inject my database-connector class on one of the routes classes, I get the error:
TypeError: Cannot read property 'mongoDBClient' of undefined
Expected Behavior
My expected behaviour should be to normally inject a class into other classes, if i take into consideration that my inversify-config is setted properly.
Current Behavior
Inversify does not inject my class properly. Node detects injected class as
undefined
.Steps to Reproduce (for bug)
inversify config
entry-point file
express.file
abstract route class
one of the route files
Error is thrown here
Context
The error is thrown in my route file in the
checkdLogggedIn
method. Normally i should be able to import the mongoDBClient class. Be aware that i import the mongoDBClient also in the parent Express class.Your Environment
Versions:
Stack trace