最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

为什么出现此错误“错误:已经将握手加入队列后无法加入握手。”?

IT培训 admin 6浏览 0评论

为什么出现此错误“错误:已经将握手加入队列后无法加入握手。”?

嘿,伙计们,我只是想制作一个简单的表单,将数据发送到mySQL数据库。我的问题是我提交后无法再提交一个。它只允许我提交一次表单,然后在第二次提交表单后出现此错误“错误:已经将握手入队后无法将握手入队”。我在线上看过,似乎每个表格提交后我都需要重新启动与mysql的连接。我曾尝试将其放入函数中,但似乎无法使其正常工作。

//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");
回答如下:

您正在多次连接,而没有关闭连接。我使用连接池。您这样做的方式可能会导致大量开放连接建立,这将使服务器瘫痪。

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

为什么出现此错误“错误:已经将握手加入队列后无法加入握手。”?

嘿,伙计们,我只是想制作一个简单的表单,将数据发送到mySQL数据库。我的问题是我提交后无法再提交一个。它只允许我提交一次表单,然后在第二次提交表单后出现此错误“错误:已经将握手入队后无法将握手入队”。我在线上看过,似乎每个表格提交后我都需要重新启动与mysql的连接。我曾尝试将其放入函数中,但似乎无法使其正常工作。

//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");
回答如下:

您正在多次连接,而没有关闭连接。我使用连接池。您这样做的方式可能会导致大量开放连接建立,这将使服务器瘫痪。

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();
    });
  });
});
发布评论

评论列表 (0)

  1. 暂无评论