节点js+Mysql:由于致命错误而无法入队

Node js + Mysql : cannot enqueue because of fatal errors

本文关键字:致命错误 js+Mysql 节点      更新时间:2023-09-26

我正在使用node js(更具体地说是express js(运行一个应用程序,并使用mysql客户端保存一些数据。

它完美地工作了一段时间,没有任何问题,但后来我突然出现了这些错误:

跟踪:出现致命错误后无法将Quit排入队列。协议_validateEnqueue(C:''nodejs'' 12-coffure''node_modules''mysql''lib''protocol''protocol.js:218:11(协议_enqueue(C:''nodejs'' 12-coffure''node_modules''mysql''lib''protocol''protocol.js:135:13(在Protocol.quit上(C:''nodejs''十二个coffure''node_modules''mysql''lib''Protocol.js:88:23(在PoolConnection.end(C:''nodejs''十二个coffure''node_modules''mysql''lib''Connection.js:255:18(在Pool_purgeConnection(C:''nodejs''十二个coffure''node_modules''mysql''lib''Pool.js:259:16(在PoolConnection_removeFromPool(C:''nodejs''steely-coffure''node_modules''mysql''lib''PoolConnection.js:70:8(在PoolConnection。(C:''nodejs''十二个coffure''node_modules''mysql''lib''PoolConnection.js:29:12(在emitOne(events.js:77:13(位于PoolConnection.emit(events.js:169:7(在PoolConnection.Connection.handleProtocolError(C:''nodejs''十二个coffure''node_modules''mysql''lib''Connection.js:439:8(跟踪:出现致命错误后无法将查询排入队列。协议_validateEnqueue(C:''nodejs'' 12-coffure''node_modules''mysql''lib''protocol''protocol.js:218:11(协议_enqueue(C:''nodejs'' 12-coffure''node_modules''mysql''lib''protocol''protocol.js:135:13(在PoolConnection.query中(C:''nodejs''十二个coffure''node_modules''mysql''lib''Connection.js:214:25(在Object.fetchDatas上(C:''nodejs''十二个coffure''public''javascripts''twelvebabase.js:94:12(在C:''nodejs''十二个发型''路线''索引.js:7:7在Layer.handle[作为handle_request](C:''nodejs''十二个coffure''node_modules''express''lib''router''Layer.js:95:5(在下一个(C:''nodejs'' 12-coffure''node_modules''express''lib''router''route.js:131:13(在Route.dispatch(C:''nodejs''十二个coffure''node_modules''express''lib''router''Route.js:112:3(在Layer.handle[作为handle_request](C:''nodejs''十二个coffure''node_modules''express''lib''router''Layer.js:95:5(位于C:''nodejs''十二个coffure''node_modules''express''lib''router''index.js:277:22C: ''nodejs''十二发型''public''javascripts''twelvebabase.js:26抛出(消息(;^

错误:出现致命错误后无法将查询排入队列。协议_validateEnqueue(C:''nodejs'' 12-coffure''node_modules''mysql''lib''protocol''protocol.js:199:16(协议_enqueue(C:''nodejs'' 12-coffure''node_modules''mysql''lib''protocol''protocol.js:135:13(在PoolConnection.query中(C:''nodejs''十二个coffure''node_modules''mysql''lib''Connection.js:214:25(在Object.fetchDatas上(C:''nodejs''十二个coffure''public''javascripts''twelvebabase.js:94:12(在C:''nodejs''十二个发型''路线''索引.js:7:7在Layer.handle[作为handle_request](C:''nodejs''十二个coffure''node_modules''express''lib''router''Layer.js:95:5(在下一个(C:''nodejs'' 12-coffure''node_modules''express''lib''router''route.js:131:13(在Route.dispatch(C:''nodejs''十二个coffure''node_modules''express''lib''router''Route.js:112:3(在Layer.handle[作为handle_request](C:''nodejs''十二个coffure''node_modules''express''lib''router''Layer.js:95:5(在C:''nodejs''十二个coffure''node_modules''express''lib''router''index.js:277:22

问题是,我真的找不到代码中的问题,我甚至不知道为什么有时在工作中,有时不在。

这是我启动数据库的代码:

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);
                                }
                            });
                        }
                    });
                }
            });
        }
    });
}
}
}

当然,我从另一个文件调用fetchData((来获取要显示的所有数据。

最后,我放弃了纯MYSQL,转而使用Sequelize。这是我的代码(它终于工作了(:

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;

然后,我在上一个代码段的最后一行导出了另一个调用函数"synchronize"的文件("exports.synchronize=synchronizeBdd"(:

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

对我来说,我之所以出现这个错误,只是因为MySql服务器没有运行,所以最好检查