0
  1. I want to use myslq based on node js to communicate with the current Android server. There is a problem in trying to parse json to send data to the server.

    app.get('/main', function(req, res) {
       if (req.cookies.auth) {
          fs.readFile('main.html', function(err, data) {
          connection.query('SELECT * from datework', function(err, rows) {
          if (err) throw err;
    
          console.log('rows: ', rows);
          console.log('json parsing...')
          res.json(rows);
    
          var st = rows[0].start_time;
          var et = rows[0].end_time;
          res.json(st+" , "+ et);
          console.log(st+" , "+ et);
          });
       });
    }});
    

The entire row of the query statement appears, but I want to specify only start_time and end_time.

  1. Finally, I want only start_time_ and end_time.

C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Parser.js:79 throw err; // Rethrow non-MySQL errors ^

Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) at ServerResponse.header (C:\Users\KTH_LAP\Desktop\G.D\node_modules\express\lib\response.js:725:10) at ServerResponse.json (C:\Users\KTH_LAP\Desktop\G.D\node_modules\express\lib\response.js:253:10) at Query._callback (C:\Users\KTH_LAP\Desktop\G.D\app.js:56:13) at Query.Sequence.end (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24) at Query._handleFinalResultPacket (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\sequences\Query.js:137:8) at Query.EofPacket (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\sequences\Query.js:121:8) at Protocol._parsePacket (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Protocol.js:280:23) at Parser.write (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Parser.js:75:12) at Protocol.write (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Protocol.js:39:16)

2
  • Error happens because you send the response twice with res.json(). Commented Jun 11, 2017 at 9:33
  • Ah Ha! Thank you!! Commented Jun 11, 2017 at 11:24

1 Answer 1

0

It is not a problem related to MySQL at all.

When you make a request to your express backend application, you must send a single response accordingly, with for exemple res.json() or res.send(). You can't send multiple response.

In your function you send it twice, this is why you get the error

Error: Can't set headers after they are sent.

You can just comment the line // res.json(rows); in your code to correct this error.

See this answer that explain it much better than I do.

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.