简体   繁体   中英

Why am I getting this error “Error: Cannot enqueue Handshake after already enqueuing a Handshake.”?

Hey guys and gals I am just trying to make a simple form that sends data to mySQL database. My problem is after I submit it I can't submit another one. It only allows me to submit the form once, and then after submitting it the second time I get this error "Error: Cannot enqueue Handshake after already enqueuing a Handshake." I have looked online and it seems that I need to restart the connection to mysql after each form submits. I have tried putting into a function, but can't seem to get it to work.

 //require packages var express = require("express"); var app = express(); var path = require("path"); var mysql = require("mysql"); var bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); //connect to our database var con = mysql.createConnection({ host: "localhost", user: "root", password: "yourRootPassword", database: "mydb" }); //joining index.html to get route app.get("/", function(req, res) { res.sendFile(path.join(__dirname + "/index.html")); }); //setting post route to /submit >> how we post to database>> app.post("/submit", function(req, res) { //req.body.nameOfInput var name = req.body.name; var email = req.body.email; var username = req.body.username; res.write('You sent the name "' + req.body.name + '".\n'); res.write('You sent the email "' + req.body.email + '".\n'); res.write('You sent the username "' + req.body.username + '".\n'); //inserting data into sql var con.connect(function(err) { if (err) throw err; var sql = "INSERT INTO form (name, email,username) VALUES ('" + name + "', '" + email + "','" + username + "')"; con.query(sql, function(err, result) { if (err) throw err; console.log("1 record inserted"); res.end(); }); }); }); app.listen(3000); console.log("Running at Port 3000");

You are connecting multiple times, without closing the connection. I use connection pools. The way you are doing it could cause massive build up of open connections, which will bring down server. I would also do a res.status(200).send("Insert completed"), but it looks like you have some debugging code.

var myPool;
function connect() {
    return new Promise((resolve, reject) => {
        pool = mysql.createPool({
            connectionLimit: 10,
            host     : this.host,
            user     : this.user,
            password : this.password,
            database : this.database
        });
        resolve(pool);
    });
}

connect().then(pool => {
    myPool = pool;
})

app.post("/submit", function(req, res) {
  //req.body.nameOfInput
  var name = req.body.name;
  var email = req.body.email;
  var username = req.body.username;
  res.write('You sent the name "' + req.body.name + '".\n');
  res.write('You sent the email "' + req.body.email + '".\n');
  res.write('You sent the username "' + req.body.username + '".\n');
  //inserting data into sql var
  myPool.getConnection((err, con) => {
    if (err) throw err;
    var sql =
      "INSERT INTO form (name, email,username) VALUES ('" +
      name +
      "', '" +
      email +
      "','" +
      username +
      "')";
    con.query(sql, function(err, result) {
      if (err) throw err;
      console.log("1 record inserted");
      con.release();
      res.end();
    });
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM