【问题标题】:How to use mysql-based node js (ft.json parsing)如何使用基于mysql的节点js(ft.json解析)
【发布时间】:2017-06-11 08:33:01
【问题描述】:
  1. 我想使用基于node js的myslq与当前的Android服务器进行通信。尝试解析json向服务器发送数据时出现问题。

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

查询语句的整行出现了,但我只想指定start_time和end_time。

  1. 最后,我只想要 start_time_ 和 end_time。

C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Parser.js:79 抛出错误; // 重新抛出非 MySQL 错误 ^

错误:发送后无法设置标头。 在 ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) 在 ServerResponse.header (C:\Users\KTH_LAP\Desktop\G.D\node_modules\express\lib\response.js:725:10) 在 ServerResponse.json (C:\Users\KTH_LAP\Desktop\G.D\node_modules\express\lib\response.js:253:10) 在 Query._callback (C:\Users\KTH_LAP\Desktop\G.D\app.js:56:13) 在 Query.Sequence.end (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24) 在 Query._handleFinalResultPacket (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\sequences\Query.js:137:8) 在 Query.EofPacket (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\sequences\Query.js:121:8) 在 Protocol._parsePacket (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Protocol.js:280:23) 在 Parser.write (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Parser.js:75:12) 在 Protocol.write (C:\Users\KTH_LAP\Desktop\G.D\node_modules\mysql\lib\protocol\Protocol.js:39:16)

【问题讨论】:

  • 发生错误是因为您使用res.json() 发送了两次响应。
  • 啊哈!谢谢!!

标签: javascript android mysql json node.js


【解决方案1】:

这根本不是与 MySQL 相关的问题。

当您向 express 后端应用程序发出请求时,您必须相应地发送一个响应,例如 res.json() 或 res.send()。 您不能发送多个响应。

在你的函数中你发送了两次,这就是你得到错误的原因

错误:发送后无法设置标头。

您只需在代码中注释 // res.json(rows); 行即可更正此错误。

请参阅this 答案,该答案比我解释得好。

【讨论】:

    猜你喜欢
    • 2017-12-13
    • 2017-11-15
    • 2018-10-23
    • 2016-08-03
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多