错误:使用NodeJS在MySQL中调用退出后无法排队查询

时间:2019-11-03 15:58:19

标签: mysql node.js

我正在尝试运行此NodeJs代码以在Mysql DB中添加数据:

app.post("/taxi",(req,res)=>{
    let model=req.body.model;
    let numberplate=req.body.numberplate;
    let brand= req.body.brand;
    let yearOfPur=req.body.yearOfPur;
    let params=[model,numberplate,brand,yearOfPur];
    //adding data
    //console.log(params);
    let sql = "INSERT INTO taxi (model,numberplate,brand,yearOfPur) VALUES ?";

    connection.query(sql,params,(err)=>{
        if(err){console.log(err);}
        //ending connection
        res.send("data added");
    });
    // connection.end();

})

但是显示错误。这是完整的错误

{ Error: Cannot enqueue Query after invoking quit.
    at Protocol._validateEnqueue (/home/ishdutt/WebDev/DBMSProject/node_modules/mysql/lib/protocol/Protocol.js:215:16)
    at Protocol._enqueue (/home/ishdutt/WebDev/DBMSProject/node_modules/mysql/lib/protocol/Protocol.js:138:13)
    at Connection.query (/home/ishdutt/WebDev/DBMSProject/node_modules/mysql/lib/Connection.js:201:25)
    at app.post (/home/ishdutt/WebDev/DBMSProject/app.js:65:16)
    at Layer.handle [as handle_request] (/home/ishdutt/WebDev/DBMSProject/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/ishdutt/WebDev/DBMSProject/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/ishdutt/WebDev/DBMSProject/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/ishdutt/WebDev/DBMSProject/node_modules/express/lib/router/layer.js:95:5)
    at /home/ishdutt/WebDev/DBMSProject/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/ishdutt/WebDev/DBMSProject/node_modules/express/lib/router/index.js:335:12) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }

在跟随许多Post之后,我尝试对同一错误发表评论connection.end(),但该错误仍然存​​在。我该怎么办?

1 个答案:

答案 0 :(得分:0)

最后,我理解了这个问题。这是因为我试图使用connection.end()(在创建表期间)关闭连接,此后,我正在运行MySQL查询以获取数据。从该查询中删除connection.end()后,现在可以正常使用了。