0

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



    },

//services 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)
  • don't use res.json in for loop, accumulate the result in for loop and then use res.json after for loop – Vikash Dahiya Nov 03 '17 at 10:57
  • Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Mika Sundland Nov 03 '17 at 12:09
  • If you want to call async functions in a loop, I recommend you to use `eachOf` from `async` package. See the documentation here, http://caolan.github.io/async/docs.html#eachOf – giankotarola Nov 03 '17 at 12:25

1 Answers1

0

You cannot loop responses per one request, it's illegal in Express, returning more than one response per one request will crash your NodeJS (Express will return an unhandledException). Loop, insert the data into an array then res send values from the array (out of the loop, of course).

Milan Velebit
  • 1,933
  • 2
  • 15
  • 32