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)

Pengyy
  • 37,383
  • 15
  • 83
  • 73
Taehyeon
  • 31
  • 1
  • 9

1 Answers1

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.

TGrif
  • 5,725
  • 9
  • 31
  • 52