Express - MySQL conn.end()不起作用(调用quit后无法入队)

时间:2018-05-18 14:19:34

标签: mysql node.js express passport.js

我已阅读帖子Node Mysql Cannot Enqueue a query after calling quitconn.end()位于我的查询栏中。

我的问题是我的conn.end中的dashboard.js无法正常工作,并因以下错误而破坏了应用。

我需要使用它,因为MySQL连接充斥着系统,直到数据库停止接受更多这些连接,因为每次使用它们都会保持打开状态。

对于这篇文章,我将只使用我的NodeJS应用程序的几个路由中的一个(dashboard.js)。

`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

需要外部演示here

0 个答案:

没有答案