Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Passing Model in Constructor to another Class

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"
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!