1

I'm using NodeJS with express, consign, body-parser, etc.

I'm trying to load a module in a var, so i can use their functions, but i'm getting a: TypeError: Cannot read property 'listar' of undefined at app.get (/home/sartori/Documentos/Projetos/react-system/api/controllers/autores.js:6:11) at Layer.handle [as handle_request] (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/layer.js:95:5) at next (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/layer.js:95:5) at /home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/index.js:335:12) at next (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/index.js:275:10) at /home/sartori/Documentos/Projetos/react-system/api/node_modules/express-validator/lib/express_validator.js:250:5 at Layer.handle [as handle_request] (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/layer.js:95:5)

Here's my controller:

module.exports = app => {
app.get('/api/autores', (req,res) => {
    let connection = app.connection.connectionFactory();
    let autores = app.models.AutoresDao(connection);

    autores.listar((error,result) => {
        if(error) {
            console.log('Erro ao listar os autores: ' + error);
            res.status(500).send(error);
        } else {
            res.json(result);
        }
    });

});

}

Here's the AutoresDao:

function AutoresDao(connection) {
    this._connection = connection;
}

AutoresDao.prototype.listar = callback => this._connection.query('SELECT * FROM autores', callback);

module.exports = function() {
    return AutoresDao;
}

The problem is: Cannot read property 'listar' of undefined.

Apparently the problem is loading the AutoresDao file.

I'm 100% sure of files path, AutoresDao is inside models folder, already loaded on consign. The same for the controller, connection, express config etc.

Any idea of what i'm doing wrong?

1 Answer 1

1

You need to be using the 'new' keyword if you are going to format your object constructor function in that manner. You will have to reformat your AutoresDao function to explicitly return an object, or use the 'new' keyword when you assign it to autores in the controller.

Please checkout this article for an explanation of the different instantiation patterns.

https://medium.com/dailyjs/instantiation-patterns-in-javascript-8fdcf69e8f9b

Hope that helps! Cheers.

1
  • 1
    Yes, this fixed my problem. Thanks for the link too, very helpful! Jun 14, 2018 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.