如何解决MySQL数据库中的second q1wery 错误?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (94)

在更新页面(重新查询)之后,收到错误:

    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 throw er; // Unhandled 'error' event ^

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)
提问于
用户回答回答于

将传递项添加到视图中:

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

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动