首页 文章

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

提问于
浏览
1

我正在运行一个带有节点js的应用程序(更具体地说是表达js),我用mysql客户端保存了一些数据 .

它完美地工作了一段时间没有问题,但随后所有的suddden我得到这些错误:

跟踪:致命错误后无法排队退出 . 在Protocol._enqueue(C:\ nodejs \ 12-coiffure \ node_modules \ mysql \ lib \)的Protocol._validateEnqueue(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ protocol \ Protocol.js:218:11)中协议\ Protocol.js:135:13)在PoolConnection.end(C:\ nodejs \)的Protocol.quit(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ protocol \ Protocol.js:88:23)在PoolConnection._removeFromPool的Pool._purgeConnection(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ Pool.js:259:16)中的12-coiffure \ node_modules \ mysql \ lib \ Connection.js:255:18) PoolConnection上的(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ PoolConnection.js:70:8) . (C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ PoolConnection.js:29:12)在PoolConnection的PoolConnection.emit(events.js:169:7)的emitOne(events.js:77:13)处.Connection._handleProtocolError(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ Connection.js:439:8)跟踪:致命错误后无法将查询排入队列 . 在Protocol._enqueue(C:\ nodejs \ 12-coiffure \ node_modules \ mysql \ lib \)的Protocol._validateEnqueue(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ protocol \ Protocol.js:218:11)中协议\ Protocol.js:135:13)在Object.fetchDatas(C:\ nodejs \ 12-)的PoolConnection.query(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ Connection.js:214:25) coiffure \ public \ javascripts \ twelvebase.js:94:12)在C:\ nodejs \ twelve-coiffure \ routes \ index.js:7:7在Layer.handle [as handle_request](C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ layer.js:95:5)在Route.dispatch的下一个(C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ route.js:131:13) C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ route.js:112:3)在Layer.handle [as handle_request](C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ layer.js:95:5)在C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ index.js:277:22 C:\ nodejs \ twelve-coiffure \ public \ javascripts \ twelvebase.js :26投掷(留言); ^错误:致命错误后无法排队查询 . 在Protocol._enqueue(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \)的Protocol._validateEnqueue(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ protocol \ Protocol.js:199:16)中协议\ Protocol.js:135:13)在Object.fetchDatas(C:\ nodejs \ 12-)的PoolConnection.query(C:\ nodejs \ twelve-coiffure \ node_modules \ mysql \ lib \ Connection.js:214:25) coiffure \ public \ javascripts \ twelvebase.js:94:12)在C:\ nodejs \ twelve-coiffure \ routes \ index.js:7:7在Layer.handle [as handle_request](C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ layer.js:95:5)在Route.dispatch的下一个(C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ route.js:131:13) C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ route.js:112:3)在Layer.handle [as handle_request](C:\ nodejs \ twelve-coiffure \ node_modules \ express \ lib \ router \ layer.js:95:5)在C:\ nodejs \ twelve-coiffure \ 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);
                                }
                            });
                        }
                    });
                }
            });
        }
    });
}
}
}

当然我从另一个文件调用fetchDatas()来获取要显示的所有数据 .

2 回答

  • 0

    最后,我放弃了普通的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()
    })
    
  • 1

    对我来说,我得到这个错误只是因为MySql服务器没有运行,所以检查它是一个好主意

相关问题