0

I am working on a controller that is using express. I believe the issue is the way the project is setup is that the routes are initialized in the constructor. But I am not 100%, but when I console.log(this._messageService), I get undefined (this is when I call the route directly using postman).

[1] (node:30820) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_messageService' of undefined

index.ts

export const app = new App(
  [
    new MessageController(new MessageService()),
  ],
  8080
);

app.listen();

message.controller.ts

class MessageController implements IController {
 public path = "/message";
 public router = express.Router();
 public intializeRoutes() {
   this.router.get("/", this.get);
 }

 constructor(private readonly _messageService: MessageService) {
   this.intializeRoutes();
 }

 async get(req: Request, res: Response) {
   console.log(this._messageService); <=== 
   const data = await this._messageService.getAll();
   res.send(data);
 }
}

export default MessageController;
Snoopy
  • 1,257
  • 2
  • 19
  • 32

0 Answers0