I am encountering an issue with my HomeController
and its index method. I have declared a variable called `this.data` in the constructor, but when I try to access it within the index method, I get the following error message:
TypeError: Cannot read property 'data' of undefined
Here is my implementation of the HomeController
:
import Controller from "./../../vendor/controller";
import * as express from "express";
import hException from "./../helper/exception.helper";
class HomeController extends Controller {
data:object
constructor(){
super()
this.data = {
tes: 'tes'
}
}
async index(request:express.Request, response:express.Response, next:express.NextFunction):Promise<any>{
try {
let template = 'home/index';
//next to viewer
response.render(template, { layout: 'dashboard', data: this.data});
} catch (error) {
next(new hException(error))
}
}
}
export default HomeController
The Routes
file that invokes the HomeController
:
import * as express from "express";
import Controller from "./../../controller/home.controller";
import { Jwt as MJwt } from "./../../middleware/auth.middleware";
class Routes {
private router:express.Router = express.Router()
private path:string = '/'
private controller:Controller
private MJwt = new MJwt()
constructor() {
this.controller = new Controller;
this.run()
}
public run(){
this.router.route(this.path)
.get(
this.controller.index
);
}
}
export default Routes