简体   繁体   中英

NodeJS Express MySQL post request throwing “Cannot set headers after they are sent to the client”

I try to send some MySQL results back via a express post request, but no matter what I try, there's always the following error:

(node:3743) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:158:21)
    at /home/dvs23/projects/Group/Visualizer/index.js:193:11
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:3743) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3743) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The important part of the code is:

app.post('/getNews', (req, res) => {
  var prom = new Promise(function(resolve, reject) {
      //create nodes for result
      con.connect(function() {
        console.log("Connected!");
        con.query("SELECT ID,length(Text) FROM news;", function(err, result, fields) {
          if (err) {
            console.log(err);
            reject(err);
            return;
          }

          var nodesArr = [];
          result.forEach((row) => {
            nodesArr.push({
              "id": row["ID"],
              "size": row["length(Text)"]
            });
          });

          con.query("SELECT * FROM newsSim;", function(err, result2, fields) {
            if (err){
              console.log(err);
              reject(err);
              return;
            }

            var linksArr = [];
            result2.forEach((row) => {
              linksArr.push({
                "source": row["TID1"],
                "target": row["TID2"],
                "value": row["SIM"]
              });
            });
            console.log({
              "nodes": nodesArr,
              "links": linksArr
            });
            resolve({
              "nodes": nodesArr,
              "links": linksArr
            });
          });
        });
      });
    })
    .then(function(result) {
      console.log(result);
      res.send(result); //send result to client
    }, function(err) {
      console.log("END"+err);
      res.send({
        "nodes": [],
        "links": []
      });
    });
});

App is my express-app, the request comes from a static HTML page, which is also served by the NodeJS server, via jQuery.
I already use a promise, so how is it possible send is already called?? Also without promises, just nested queries, there's the same error (obviously without the UnhandledPromiseRejectionWarning stuff).

EDIT:

It's not a problem with the nested MySQL-queries, even the following does not work:

app.post('/getNews', (req, res) => {
  //var text = req.fields["text"];

  var prom = new Promise(function(resolve, reject) {
      //create nodes for result
      con.connect(function() {
        console.log("Connected!");
        con.query("SELECT ID,length(Text) FROM news;", function(err, result, fields) {
          if (err) {
            console.log(err);
            reject(err);
            return;
          }

          var nodesArr = [];
          result.forEach((row) => {
            nodesArr.push({
              "id": row["ID"],
              "size": row["length(Text)"]
            });
          });

          resolve(nodesArr);
          return;
        });
      });
    })
    .then(function(news) {
      console.log(news);
      //if (res.headersSent) return;
      res.send(news); //send result to client
    }, function(err) {
      console.log("END"+err);
      //if (res.headersSent) return;
      res.send({
        "nodes": [],
        "links": []
      });
    });
});

If I use the if(res.headersSent), simply nothing is sent back to my site, neither an empty result, nor the real result, which is fetched as wanted -> I can log it to console...

EDIT2:

app.post('/getNews', (req, res) => {
  //var text = req.fields["text"];
  req.connection.setTimeout(6000000, function () { 
    console.log("Timeout 2");
    res.status(500).end(); 
  });

  var prom = new Promise(function(resolve, reject) {
      //create nodes for result
      con.connect(function() {
        console.log("Connected!");
        con.query("SELECT ID,length(Text) FROM news;", function(err, result, fields) {
          if (err) {
            console.log(err);
            reject(err);
            return;
          }

          var nodesArr = [];
          result.forEach((row) => {
            nodesArr.push({
              "id": row["ID"],
              "size": row["length(Text)"]
            });
          });

          con.query("SELECT * FROM newsSim;", function(err2, result2, fields2) {
            if (err2){
              console.log(err2);
              reject(err2);
              return;
            }

            var linksArr = [];
            result2.forEach((row) => {
              linksArr.push({
                "source": row["TID1"],
                "target": row["TID2"],
                "value": row["SIM"]
              });
            });
            resolve({
              "nodes": nodesArr,
              "links": linksArr
            });
            return;
          });
        });
      });
    })
    .then(function(news) {
      //if (res.headersSent) return;
      res.send(news); //send result to client
      console.log(news);
    }, function(err) {
      console.log("END"+err);
      //if (res.headersSent) return;
      res.send({
        "nodes": [],
        "links": []
      });
    });
});

Tried to set timeout, but Now the error occurs before the timeout callback is called...

(node:9926) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (/home/dvs23/projects/Group/Visualizer/node_modules/express/lib/response.js:158:21)
    at /home/dvs23/projects/Group/Visualizer/index.js:198:11
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:9926) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9926) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Timeout 2

Don't ask me why, but it seems to work now... Possibly setting a specific timeout as suggested by oniramarf was the key. Also added

multipleStatements: true   

to mysql.createConnection(...) and moved static stuff to different path:

app.use('/site', express.static('static'));

but that's possibly not the reason it works now :)

Code:

app.post('/getNews', (req, res) => {
  //var text = req.fields["text"];

  var prom = new Promise(function(resolve, reject) {
      //create nodes for result
      con.connect(function() {
        console.log("Connected!");
        con.query("SELECT ID,length(Text) from news;", function(err, result, fields) { //SELECT ID,length(Text) FROM news;
          if (err) {
            console.log(err);
            reject(err);
            return;
          }

          con.query("SELECT * FROM newsSim;", function(err2, result2, fields2) {
            if (err2) {
              console.log(err2);
              reject(err2);
              return;
            }

            var imporIDs = new Set();

            var linksArr = [];
            result2.forEach((row) => {
              if (row["SIM"] > 0.25) {
                imporIDs.add(row["TID1"]);
                imporIDs.add(row["TID2"]);
                linksArr.push({
                  "source": row["TID1"],
                  "target": row["TID2"],
                  "value": row["SIM"]
                });
              }
            });

            var nodesArr = [];
            result.forEach((row) => {
              if (imporIDs.has(row["ID"])){
                nodesArr.push({
                  "id": row["ID"],
                  "size": row["length(Text)"]
                });
              }
            });
            resolve({
              "nodes": nodesArr,
              "links": linksArr
            });
            return;
          });
        });
      });
    })
    .then(function(news) {
      //if (res.headersSent) return;
      res.send(news); //send result to client
      console.log(news);
    }, function(err) {
      console.log("END" + err);
      //if (res.headersSent) return;
      res.send({
        "nodes": [],
        "links": []
      });
    });
});

var server = http.createServer(app);

server.listen(8080, function() {
  console.log('localhost:8080!');
});
server.timeout = 120000;

Edit:
Really seems to be the timeout, after some changes the error occurred again, so I set server.timeout = 12000000 and it worked again :)

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