I'm working on small project for school and I'm new to express and dependency injection. I started this little project just to get an idea of how it works. I tried to understand this error for hours but I didnt find something about it. Thank you for your help !
TypeError: Cannot read properties of undefined (reading 'service')
at controllerMethod (D:\Projects\Typescript\third\dist\app.controller.js:20:14)
at Layer.handle [as handle_request] (D:\Projects\Typescript\third\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Projects\Typescript\third\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (D:\Projects\Typescript\third\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (D:\Projects\Typescript\third\node_modules\express\lib\router\layer.js:95:5)
at D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:284:15
at Function.process_params (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:346:12)
at next (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:280:10)
at Function.handle (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:175:3)
at router (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:47:12)
Here is the code :
//app.ts
import express, { json, urlencoded } from 'express'
import { APPROUTE } from './app.route'
const app = express()
app.use(json())
app.use(urlencoded({ extended: false }))
app.use(APPROUTE)
app.listen(5000, ()=>{
console.log(`connected on port 5000`)
})
//app.controller.ts
import { Request, Response } from "express";
import { inject, singleton } from "tsyringe";
import { AppService } from "./app.service";
@singleton()
export class AppController{
constructor(@inject(AppService) private service: AppService){}
controllerMethod(req: Request, res: Response){
this.service.methodService()
}
}
//app.service.ts
import { singleton } from "tsyringe";
@singleton()
export class AppService{
methodService(){
console.log('service working')
}
}
//app.route.ts
import 'reflect-metadata'
import { Router } from "express";
import { container } from "tsyringe";
import { AppController } from "./app.controller";
export const APPROUTE = Router()
const controller: AppController = container.resolve<AppController>(AppController)
APPROUTE.get('/', controller.controllerMethod)
// package.json
{
"scripts": {
"start": "nodemon ./dist/app.js"
},
"devDependencies": {
"nodemon": "^2.0.16"
},
"dependencies": {
"@types/express": "^4.17.13",
"@types/node": "^18.0.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.7.0"
}
}
//tsconfig.json
{
"compilerOptions": {
"target": "es6",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictPropertyInitialization": false,
"skipLibCheck": true
}
}
CodePudding user response:
Just change APPROUTE.get('/', controller.controllerMethod)
to
APPROUTE.get('/', (req, res)=>controller.controllerMethod(req, res))