Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My issue is that conn.end() in my initial route dashboard.js does not work and breaks the app with the following error.

I need to use it because MySQL connections are flooding the system till Database stop accepting more of them since they all stay open each time that are used.

For this post I will use only my initial route, the one that renders after the user logs in (dashboard.js)
`Event.js` is not a part of my working files, probably is a file from `./node_modules`

events.js:183 
      throw er; // Unhandled 'error' event
      ^

Error: Cannot enqueue Quit after invoking quit.
    at Protocol._validateEnqueue (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:204:16)
    at Protocol._enqueue (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:139:13)
    at Protocol.quit (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:92:23)
    at Connection.end (E:\NodeJS\Project-Change\node_modules\mysql\lib\Connection.js:249:18)
    at ServerResponse.res.end (E:\NodeJS\Project-Change\node_modules\express-myconnection\lib\express-myconnection.js:114:54)
    at ServerResponse.send (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:191:8)
    at fn (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:896:10)
    at View.exports.renderFile [as engine] (E:\NodeJS\Project-Change\node_modules\ejs\lib\ejs.js:323:3)
    at View.render (E:\NodeJS\Project-Change\node_modules\express\lib\view.js:76:8)
    at Function.app.render (E:\NodeJS\Project-Change\node_modules\express\lib\application.js:527:10)
    at ServerResponse.res.render (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:900:7)
    at Query._callback (E:\NodeJS\Project-Change\routes\dashboard.js:39:17)
    at Query.Sequence.end (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
    at Query._handleFinalResultPacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
    at Query.EofPacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
    at Protocol._parsePacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:279:23)

app.js (only relative lines)

var express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    app = express(),
    expressValidator = require('express-validator'),
    session = require('express-session'),
    passport = require('passport'),
    flash = require('connect-flash'),
    passportConfig = require('./config/passport'),
    dbConfig = require('./config/db');

// skipping code about static files, bodyparser, expressValidator, session, passport

passportConfig(passport)

/*MySQL connection*/
var connection = require('express-myconnection'),
    mysql = require('mysql');

app.use(
    connection(mysql, dbConfig, 'request')
);

var dashboard = require('./routes/dashboard.js'); // in this route I apply conn.end()
var router = require('./routes/rates.js');

// skipping app.post/app.get code for /login & /logout & isLoggedIn middleware

app.use('/', router);
app.use('/', dashboard); // issue on that

app.get('/', function(req, res) {
    res.render('./dashboard.ejs'); //issue on that
});

module.exports = app;

routes/dashboard.js - route where I put conn.end()

var express = require('express');
var router = express.Router();

var dashboard = express.Router();

dashboard.use(function(req, res, next) {
    console.log(req.method, req.url);
    next();
});

var dashboard = router.route('/');


//show the CRUD interface | GET
dashboard.get(function(req, res, next) {

    req.getConnection(function(err, conn) {

        if (err) return next("Cannot Connect");

        var query = conn.query('SELECT SUM(total_price) AS transactions_total FROM transactions WHERE date_created = CURDATE(); SELECT SUM(total_profit) AS total_profit FROM transactions', function(err, rows) {


            if (err) {
                console.log(err);
                return next("MySQL error, check your query");
            }

            var ab = { data: rows[0] };
            var total_profit = { data: rows[1] };

            res.render('dashboard', { ab, total_profit });

            conn.end(); // causes the described error

        });

        // conn.end(); even tried here

    });

});

dashboard.all(function(req, res, next) {
    console.log("route for dashboard executed");
    console.log(req.params);
    next();
});


module.exports = router;
console.log('dashboard.js loaded!');


config/db.js

module.exports = {
          host     : 'localhost',
          user     : 'root',
          password : '',
          database : 'mydb',
          multipleStatements: true,
          debug    : false
}

config/passport.js

External presentation is here in case is needed

.

What I have tried:

If I check the MySQL connection before the try/catch block I get the following result

dashboard.get(function(req, res, next) {

    req.getConnection(function(err, conn) {

        console.log('\nConnection State: ', conn.state, '\n') // authenticated
        console.log('\nthreadId: ', conn.threadId, '\n') // 2856

        if (err) return next("Cannot Connect");
        
        // rest of the code
     });


However if it matters, dashboard.js route is the initial page that is rendered after the user logs in.

.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900