Arthur Deschamps Arthur Deschamps - 3 months ago 36
Javascript Question

Node js + Mysql : canno't enqueue because of fatal errors

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.

I'm really lost and have been looking for a solution for more than 4 hours on every existing forum.. Please help me and thank you by advance.

Answer

Finally, I droped out plain MYSQL and used Sequelize instead. Here is my code (which eventually 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 at the last line of code ("exports.synchronize = synchronizeBdd"):

var bdd = require('fileWithSequelizeInstantiation')

bdd.synchronize(function() {
 //here I call the functions related to my database. In my case: bdd.fetchDatas()
})