Pretty straight forward code. I am importing a class, instantiating it with some values and calling a method on it. When the method is called, I'm expecting it to make use of the initialized values I passed it earlier.
However, when I'm in the instance method, I'm getting this
as undefined
.
The specific error that I'm getting in this case: Cannot read property 'model' of undefined
const utilities = require('../../components/utilities');
class BaseController {
constructor(model) {
this.model = model;
}
show(req, res) {
return this.model.query()
.findById(req.params.id)
.then(item => {
if (!item) return utilities.throwNotFonud(res);
return utilities.responseHandler(null, res, 200, item);
})
.catch(err => utilities.responseHandler(err, res));
}
}
module.exports = BaseController;
const express = require('express');
const BaseController = require('../core/base.controller');
const Category = require('./category.model');
const controller = new BaseController(Category);
const router = express.Router();
router.get('/:id', controller.show);
module.exports = router;