Database is not connecting!

Asked

Viewed 54 times

3

My database isn’t connecting. When doing a refctoring of my code following the MVC standard the database stopped working.

Follow link with the test files: https://github.com/LeonardoVini/node-refctoring

app js.

const app = require('./config/server')

// port must be set to 9095 because incoming http requests are routed from port 80 to port 9095
app.listen(9095, function () {
    console.log('Node app is running on port 9095');
});

server.js

const express = require('express');
const consign = require('consign')
const bodyParser = require('body-parser');
const cors = require('cors');

const corsOptions = {
    origin: 'http://localhost:4200',
    optionsSuccessStatus: 200
}

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors(corsOptions))

consign()
    .include('app/routes')
    .then('config/dbConnection.js')
    .then('app/models')
    .then('app/controllers')
    .into(app);

module.exports = app;

dbConnection.js

const mysql = require('mysql')

const connMySQL = () => {
    return mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'eqix1998',
        database: 'quotedb',
        port: 3306
    });
}

module.exports = () => {
    return connMySQL;
}

Routes > Quotes.js

module.exports = (application) => {
    application.get('/quotes', (req, res) => {
        application.app.controllers.quotes.quotes(application, req, res);
    });
}

Controllers > Quotes.js

module.exports.quotes = (application, req, res) => {
    var connection = application.config.dbConnection();
    var quotesDAO = new application.app.models.QuotesDAO(connection);

    quotesDAO.getQuotes((error, results) => {
        res.send(results)
    });
}

Models > Quotes.js

function QuotesDAO(connection){
    this._connection = connection;
    console.log(this._connection)
}

QuotesDAO.prototype.getQuotes = (callback) => {
    this._connection.query('SELECT * FROM quotes', callback)
}

module.exports = () => {
    return QuotesDAO;
}

File tree:

inserir a descrição da imagem aqui

That is the mistake: inserir a descrição da imagem aqui

  • it is quite possible that it is tied to the fact that the connection is an asynchronous function.

  • It would be nice if you put the code on Github or somewhere else so we can download and make things easier, for me there seems to be a problem in the way you are doing the connMySQL module Export, but I’ll just be sure by testing the code.

  • Follow the link: https://github.com/LeonardoVini/node-refctoring

  • I use the angular for the front-end. I’m missing a connect() function. I have a code working, but all messy without following any Pattern designer and there I ran this function. However not being able to make it work in this case

1 answer

1


I checked your code and made the changes that worked for me.

Follow them:

  • First, there in the prototype, do not use Arrow-Function, for the this will not reference the object QuotesDAO, but yes to any global object.

    Would look like this:

     QuotesDAO.prototype.getQuotes = function(callback){
       this._connection.query('sua query', callback);
     }
    
  • Second, if it doesn’t work, in your database connection file, dbConnection.js, do the following:

    const connMySQL = mysql.createConnection({
     host: 'localhost',
     user: 'root',
     password: 'eqix1998',
     database: 'quotedb',
     port: 3306
    });
    connMySQL.connect(function(erro){
     if(erro)
      return console.log(erro);
      console.log('Conectou!');
    });
    
    module.exports = () => connMySQL
    

    The method connect will try to make the connection to the bank and inform if there was success. In the method you wrote, if you put a console log. the message will appear disconnect:

    console.log(connMySQL().state); // disconnect
    
  • worked here! Just change from Arrow Function to Function that ran normally. Thank you very much.

Browser other questions tagged

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