I'm creating some classes in my project made in NODEJS and in this process I made a class that will be responsible for managing standard contents, in this case I called it "Controller", below I'm placing the model.
class Controller{
constructor(_model){
this.MODEL = _model;
}
async store(req, res) {
try {
const data = await this.MODEL.create(req.body);
return res.json(data);
} catch (error) {
return res.json({
status: "error",
message: `${error.message}`,
});
}
}
async index(req, res) {
try {
const data = await this.MODEL.findAll();
return res.json(data);
} catch (error) {
return res.json({
status: "error",
message: `${error.message}`,
});
}
}
}
module.exports = Controller;
And my main Users class below:
import User from '../models/User';
import Controller from './Controller';
class Users extends Controller {
constructor(){
super(User);
}
}
export default new Users();
My file route:
import { Router } from 'express';
import UserController from '../controllers/Users';
let routerApi = Router();
routerApi.get('/', (req, res) => {
res.send('/api');
});
routerApi.get("/user", UserController.index);
routerApi.post("/user", UserController.store);
export default routerApi;
But when I call the "Users.index" method I'm getting the following error:
"Cannot read property 'MODEL' of undefined"