Typeerror: app.config.dbConnection is not a Function

Asked

Viewed 149 times

1

I’m getting the bug

TypeError: app.config.dbConnection is not a function

when refactoring a code with Consign so that the connection module with the database is executed only on the required pages.

dbconnection.js:

var mysql = require('mysql');

var connMySQL = function(){
    console.log('Conexão com banco de dados estabelecida.')
    return mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'portal_noticias',
    });
}


module.exports = function(){
    console.log('O autoload carregou o módulo de conexão com o bd');
    return connMySQL;
}

server.js:

var express = require('express');
var consign = require('consign');

var app = express();
app.set('view engine', 'ejs');
app.set('views', './public/views');

consign().include('./public/routes').then('./config/modules/database/dbconnection.js').into(app);

module.exports = app;

news js.:

module.exports = function(app){

    app.get('/news', function(req,res){

        var connection = app.config.dbConnection();

        connection.query("SELECT * FROM noticias", function(error, result){
            res.render('./sections/news/noticias', {noticias : result});
        });

    });
};

Basically, the error reports to me that dbConnection() is not a method. However, I inserted the dbConnection in the stretch consign().include('./public/routes').then('./config/modules/database/dbconnection.js').into(app);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.