mysql数据库的第二个q1wery出错

x6yk4ghg  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(211)

当我在启动服务器后发送第一个请求时,一切正常。但是在更新页面(重新查询)后,您会收到一个错误:

app.get('/images', function(req, res, next) {

 if (connection.state === 'disconnected'){
    connection.connect();}
        else {

    }
            var DATABASE='nodejsImage';
            var results;
            connection.query('use ' + DATABASE);
            connection.query('SELECT `id`, `imagename`, `filename`, `speed`, `move` FROM `images` WHERE 1',
                function (err, results, fields) {
                    if (err) return console.log("Error test")  //throw err;
                    else {
                        console.log('Images list');
                        console.log('----------------------------------');
                        for (var i in results) {
                            var gadget = results[i];
                            console.log(gadget.id +': '+ gadget.imagename+': '+gadget.speed+': '+gadget.move);

                        }
                        res.render('imagelist', { title: 'Imagelist',listImages:results });
                    }
                });

            connection.end();           //разобраться с ошибкой позже!!!
        });

错误消息:
events.js:165抛出器;//未处理的“错误”事件^

Error: Cannot enqueue Handshake after invoking quit.
    at Protocol._validateEnqueue (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\Protocol.js:204:16)
    at Protocol._enqueue (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\Protocol.js:139:13)
    at Protocol.handshake (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\Protocol.js:52:23)
    at Connection.connect (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\Connection.js:118:18)
    at E:\Dropbox\untitled5-upload-3\untitled5\app.js:48:16
    at Layer.handle [as handle_request] (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\express\lib\router\layer.js:95:5)
    at E:\Dropbox\untitled5-upload-3\untitled5\node_modules\express\lib\router\index.js:281:22
Emitted 'error' event at:
    at Connection._handleProtocolError (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\Connection.js:425:8)
    at Protocol.emit (events.js:180:13)
    at Protocol._delegateError (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\Protocol.js:392:10)
    at Handshake.<anonymous> (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\Protocol.js:221:10)
    at Handshake.emit (events.js:180:13)
    at Handshake.Sequence.end (E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:12)
    at E:\Dropbox\untitled5-upload-3\untitled5\node_modules\mysql\lib\protocol\Protocol.js:225:14
    at process._tickCallback (internal/process/next_tick.js:112:11)
hmtdttj4

hmtdttj41#

这是一个简单的方法来添加通行证项目,以查看。

var imageList = [];

        // Connect to MySQL database.
        var connection = getMySQLConnection();
        connection.connect();

        // Do the query to get data.
        connection.query('SELECT * FROM images', function(err, rows, fields) {
            if (err) {
                res.status(500).json({"status_code": 500,"status_message": "internal server error"});
            } else {
                // Loop check on each row
                for (var i = 0; i < rows.length; i++) {

                    // Create an object to save current row's data
                    var person = {
                        'id':rows[i].id,
                        'imagename':rows[i].imagename,
                        'filename':rows[i].filename,
                        'speed':rows[i].speed,
                        'move':rows[i].move
                    }
                    // Add object into array
                    imageList.push(person);
                }

                // Render index.pug page using array
                res.render('imagelist', { title: 'Imagelist',listImages:imageList });
            }
        });

        // Close the MySQL connection
        connection.end();

相关问题