1

I am trying to run this NodeJs code for adding data in 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();

})

But it is showing an error. Here is the complete error

{ 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 }

I tried out commenting the connection.end() after following many Post on the same error but still this error persist.What should I do?

IshduttT
  • 179
  • 1
  • 14

1 Answers1

5

Finally, I understood the problem. It was because I was trying to close the connection using connection.end() (during the creation of table) and after that, I was running the MySQL query for fetching the data. After removing the connection.end() from that query, it works fine now.

IshduttT
  • 179
  • 1
  • 14