express-mysql conn.end()未运行(调用quit后无法排队)

oyjwcjzk  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(256)

我已经读过post node mysql在调用quit后不能排队查询的消息 conn.end() 在我的查询块中。
我的问题是 conn.end 在我的 dashboard.js 无法工作并导致应用程序中断,出现以下错误。
我需要使用它,因为mysql连接充斥着系统,直到数据库停止接受更多的连接,因为它们每次都保持打开状态。
对于这个职位,我将只使用一个( dashboard.js )我的nodejs应用程序的几个路径之一。

`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(仅相对行)

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(路由)

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!');

配置/db.js

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

配置/passport.js
外部演示,以防需要

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题