1

I'm running a application with node js (more specifically express js) and I save some datas with a mysql client.

It perfectly works for some time with no issue but then all of suddden I get these erros:

Trace: Cannot enqueue Quit after fatal error. at Protocol._validateEnqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:218:11) at Protocol._enqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:135:13) at Protocol.quit (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:88:23) at PoolConnection.end (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:255:18) at Pool._purgeConnection (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Pool.js:259:16) at PoolConnection._removeFromPool (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\PoolConnection.js:70:8) at PoolConnection. (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\PoolConnection.js:29:12) at emitOne (events.js:77:13) at PoolConnection.emit (events.js:169:7) at PoolConnection.Connection._handleProtocolError (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:439:8) Trace: Cannot enqueue Query after fatal error. at Protocol._validateEnqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:218:11) at Protocol._enqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:135:13) at PoolConnection.query (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:214:25) at Object.fetchDatas (C:\nodejs\twelve-coiffure\public\javascripts\twelvebase.js:94:12) at C:\nodejs\twelve-coiffure\routes\index.js:7:7 at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at next (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at C:\nodejs\twelve-coiffure\node_modules\express\lib\router\index.js:277:22 C:\nodejs\twelve-coiffure\public\javascripts\twelvebase.js:26 throw(message); ^

Error: Cannot enqueue Query after fatal error. at Protocol._validateEnqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:199:16) at Protocol._enqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:135:13) at PoolConnection.query (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:214:25) at Object.fetchDatas (C:\nodejs\twelve-coiffure\public\javascripts\twelvebase.js:94:12) at C:\nodejs\twelve-coiffure\routes\index.js:7:7 at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at next (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at C:\nodejs\twelve-coiffure\node_modules\express\lib\router\index.js:277:22

The problem is that I really can't find where the issue comes from in my code, and I don't even know why sometimes in works and sometimes not.

Here is my code to start the database:

var mysql = require('mysql');
var dbPool = mysql.createPool({
 connectionLimit: 50,
 waitForConnections: true,
 queueLimit: 0,
 host: '*my host*',
 user: '*me*',
 password: '*my password*',
 database: '*heroku database*',
 wait_timeout:28800,
 connect_timeout:10,
 debug:true
});

dbPool.getConnection(function(err, connection) {
 if (err) throw (err);

 var db = connection;

 fetchDatas = function(callback) {

    // openDatabase();

    var servicesFemmes, servicesHommes, products;

    db.query("SELECT * FROM servicesFemmes", function(err, servicesFemmes, fields) {

        if (err) {
            callback(sendError(err));
        } else {

            db.query("SELECT * FROM servicesHommes", function(err, servicesHommes, fields) {
                if (err) {
                    callback(sendError(err));
                } else {
                    db.query("SELECT * FROM produits", function(err, products, fields) {
                        if (err) {
                            callback(sendError(err));
                        } else {
                            db.query("SELECT * FROM carousel", function(err, carousel, fields) {
                                if (err) {
                                    callback(sendError(err));
                                } else {
                                    callback(null, servicesFemmes, servicesHommes, products, carousel);
                                }
                            });
                        }
                    });
                }
            });
        }
    });
}
}
}

Of course I'm calling fetchDatas() from another file to get all the datas to display.

13
  • You probably want to use a promise library here like what Sequelize offers because this is already in "callback hell". Commented Aug 18, 2016 at 19:25
  • do you ever release the connection so that it can be reused again by the pool? + where does the db object come from? shouldn't that be the connection object instead? Commented Aug 18, 2016 at 20:03
  • ok @tadman I'm probably going to use that real soon if I don't find a solution here. I just wanted to keep simple (I had start with sqlite3 but it wasn't compatible with heroku hosting) since it's juste a simple page for a small shop. Commented Aug 18, 2016 at 21:06
  • @Johannes Merz well I do release the connection for every other sql functions I use but with this one I have a message that says the connection is already released so I figured out it was already done somewhere else (can't find exactly where). And for the db object yes sorry I'm going to edit the post: I missed the line "var db = connection" Commented Aug 18, 2016 at 21:11
  • 1
    @tadman I finally went with your advice and used Sequelize. That was perfect and solved my issues. Thank you Commented Aug 22, 2016 at 14:24

2 Answers 2

1

Finally, I gave up on plain MYSQL and started using Sequelize instead. Here is my code (which finally works):

var mysql = require('mysql');
var Sequelize = require('sequelize');

var sequelize = new Sequelize('database name', 'username', 'password', {
host: 'your host url',
dialect: 'mysql',

pool: {
    max: 10,
    min: 0,
    idle: 3000
}
});


//Models' creation
var servicesHommes = sequelize.define('servicesHommes', {
 nom: {
     type: Sequelize.STRING,
     field: 'nom'
 },
 cheveuxLongs: {
     type: Sequelize.STRING,
     field: 'cheveuxLongs'
 },
 cheveuxCourts: {
     type: Sequelize.STRING,
     field: 'cheveuxCourts'
 }
}, {
 freezeTableName: true
});

var servicesFemmes = sequelize.define('servicesFemmes', {
nom: {
    type: Sequelize.STRING,
    field: 'nom'
},
cheveuxLongs: {
    type: Sequelize.STRING,
    field: 'cheveuxLongs'
},
cheveuxCourts: {
    type: Sequelize.STRING,
    field: 'cheveuxCourts'
}
}, {
freezeTableName: true
});

var carousel = sequelize.define('carousel', {
 photo: {
     type: Sequelize.STRING,
     field: 'photo'
 }
});

var produits = sequelize.define('produits', {
  marque: {
     type: Sequelize.STRING,
     field: 'marque'
  },
  nom: {
    type: Sequelize.STRING,
    field: 'nom'
  },
  photo: {
    type: Sequelize.STRING,
    field: 'photo'
  }
}, {
freezeTableName: true
});

synchronizeBdd = function(callback) {

sequelize.sync().then(function() {
//any function querying the database should be placed here in this callback


    fetchDatas = function(callback) {

        servicesFemmes.findAll().then(function(femmes) {
            servicesHommes.findAll().then(function(hommes) {
                produits.findAll().then(function(products) {
                    carousel.findAll().then(function(carousels) {
                        callback(null, femmes, hommes, products, carousels);
                    })
                })
            })
        });
    }

    //end of sync()
    exports.fetchDatas = fetchDatas;
    callback();
});
}

exports.synchronize = synchronizeBdd;

Then I have another file calling my function "synchronize" exported on the last line of previous snippet ("exports.synchronize = synchronizeBdd"):

var bdd = require('fileWithSequelizeInstantiation')

bdd.synchronize(function() {
 //here I call the functions related to my database. In my case: bdd.fetchDatas()
})
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget you can use Promise.props to clean up code like you have here. That is useful for running multiple promises in parallel and using their combined results.
0

For me, i got this error simply because the MySql server wasn't running, so it is a good idea to check that

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.