简体   繁体   English

为什么我会收到此错误“错误:已将握手入队后无法将握手入队。”?

[英]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.大家好,我只是想制作一个简单的表格,将数据发送到 mySQL 数据库。 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.我上网查了一下,好像每次表单提交后都需要重启到mysql的连接。 I have tried putting into a function, but can't seem to get it to work.我试过放入 function,但似乎无法让它工作。

 //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.我也会做一个 res.status(200).send("Insert completed"),但看起来你有一些调试代码。

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();
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnhandledPromiseRejectionWarning:错误:已将握手入队后无法将握手入队 - UnhandledPromiseRejectionWarning: Error: Cannot enqueue Handshake after already enqueuing a Handshake node.js + mysql:“握手已经排队后无法排队握手。” - node.js + mysql: "Cannot enqueue Handshake after already enqueuing a Handshake." 使用 Node.js 和 MySQL 连续数据库插入(在已将握手入队后无法将握手入队) - Continuous database insert with Node.js and MySQL (Cannot enqueue Handshake after already enqueuing a Handshake) 调用退出错误后,具有回调功能的节点Cron变得无法入队握手 - node Cron with callback getting Cannot enqueue Handshake after invoking quit error WebSocket连接失败。 Websocket握手期间出错。响应代码403? - WebSocket connection failure. Error during Websocket handshake. Response Code 403? 无法托管WebSocket服务器-握手期间出错 - Cannot host WebSocket Server - error during handshake chrome在WebSocket握手期间获取新错误 - chrome getting new Error during WebSocket handshake 已经在套接字IO中对握手进行排队后,不能使握手排队? - cannot en queue handshake after already en queuing a handshake In socket IO? Websocket握手。 从ws到wss - Websocket handshake. From ws to wss Javascript为什么我不知道名字已经存在错误 - Javascript why am I not getting name already exist error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM