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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.