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

261
Views
Pasar modelo en constructor a otra clase

Estoy creando unas clases en mi proyecto hecho en NODEJS y en este proceso hice una clase que se encargará de administrar los contenidos estándar, en este caso lo llamé "Controller", abajo coloco el modelo.

 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;

Y mi clase principal de Usuarios a continuación:

 import User from '../models/User'; import Controller from './Controller'; class Users extends Controller { constructor(){ super(User); } } export default new Users();

Mi ruta de archivo:

 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;

Pero cuando llamo al método "Users.index" obtengo el siguiente 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!