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

时间:2017-11-03 10:50:55

标签: node.js express vue.js

select_value:function(del){             var self = this;

        self.$http.post("/sales/team_leader_id_table", {"id": del,}).then(function(res){
            var parentdata = res.body.result[0];            

        },function(err){
            alert(err);
        });



    },

//服务 router.post(' / team_leader_id_table',function(req,res,next){

for (var i = 0; i < req.body.id.length; i++) {
    console.log('SELECT * FROM `user` where id = "'+req.body.id[i]+'" ');
    connection.query('SELECT * FROM `user` where id = "'+req.body.id[i]+'" ', function (error, results, fields) {
        if (!error) {
            console.log(results);
            res.json({"status": "ok", "result": results});
        } else {
            res.json({"status": "failed", "message": error.message});
        }
    });

}

});

//error 
C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\Parser.js:80
        throw err; // Rethrow non-MySQL errors
        ^
Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:489:11)
    at ServerResponse.setHeader (_http_outgoing.js:496:3)
    at ServerResponse.header (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\express\lib\response.js:719:10)
    at ServerResponse.send (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\express\lib\response.js:164:12)
    at ServerResponse.json (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\express\lib\response.js:250:15)
    at Query._callback (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\routes\sales.js:1662:21)
    at Query.Sequence.end (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
    at Query._handleFinalResultPacket (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
    at Query.EofPacket (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
    at Protocol._parsePacket (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\Protocol.js:279:23)
    at Parser.write (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\Parser.js:76:12)
    at Protocol.write (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (C:\Users\HP 840 G3\Desktop\ERP\sp_workspace\node_modules\mysql\lib\Connection.js:103:28)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:264:12)

1 个答案:

答案 0 :(得分:0)

你无法循环每个请求的响应,这在Express中是非法的,每个请求返回多个响应会使NodeJS崩溃(Express将返回 unhandledException )。循环,将数据插入数组,然后从数组中重新发送值(当然,不在循环中)。