gpt4 book ai didi

javascript - 如何使用基于mysql的node js(ft.json解析)

转载 作者:行者123 更新时间:2023-11-29 18:48:17 24 4
gpt4 key购买 nike

  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。

  • 最后,我只想要 start_time_ 和 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)

    最佳答案

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

    当您向 Express 后端应用程序发出请求时,您必须相应地发送单个响应,例如 res.json()res.send() 。您无法发送多个回复。

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

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

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

    参见this答案比我解释得更好。

    关于javascript - 如何使用基于mysql的node js(ft.json解析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481857/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com