猿问

node express api报错

这是请求的方法


app.get('/api/:id', function (req, res) {

    connection.connect();

    connection.query('SELECT * from testmodel_url WhERE id='+req.params.id, function(err, rows, fields) {

        if (err) throw err;

        let obj={

            'status':200,

            'message':'success',

            'data':rows

        }

        res.json(obj)

    });

    connection.end();

});

使用postman第一次请求可以获得正确的结果

再次请求就会报错

https://img4.mukewang.com/5c947905000169be06270493.jpg

events.js:182

      throw er; // Unhandled 'error' event

      ^


Error: Cannot enqueue Handshake after invoking quit.

    at Protocol._validateEnqueue (C:\bin\pro\express\node_modules\_mysql@2.15.0@

mysql\lib\protocol\Protocol.js:204:16)

    at Protocol._enqueue (C:\bin\pro\express\node_modules\_mysql@2.15.0@mysql\li

b\protocol\Protocol.js:139:13)

    at Protocol.handshake (C:\bin\pro\express\node_modules\_mysql@2.15.0@mysql\l

ib\protocol\Protocol.js:52:23)

    at Connection.connect (C:\bin\pro\express\node_modules\_mysql@2.15.0@mysql\l

ib\Connection.js:130:18)

    at C:\bin\pro\express\index.js:29:16

    at Layer.handle [as handle_request] (C:\bin\pro\express\node_modules\_expres

s@4.16.3@express\lib\router\layer.js:95:5)

    at next (C:\bin\pro\express\node_modules\_express@4.16.3@express\lib\router\

route.js:137:13)

    at Route.dispatch (C:\bin\pro\express\node_modules\_express@4.16.3@express\l

ib\router\route.js:112:3)

    at Layer.handle [as handle_request] (C:\bin\pro\express\node_modules\_expres

s@4.16.3@express\lib\router\layer.js:95:5)

    at C:\bin\pro\express\node_modules\_express@4.16.3@express\lib\router\index.

js:281:22


慕姐8265434
浏览 785回答 3
3回答

繁星点点滴滴

从文字上讲connection.query是异步操作,为什么没返回结果就执行connection.end();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答