0
router.get('/getpostcode', function(req, res){
    console.log(req.query.suburb);

    var options = {
        url: 'http://apiurl?suburb=' + req.query.suburb + "&state=" + req.query.state ,
        headers: {
            'auth-key': key
        }
    }

    request(options, callback);

    function callback(error, response, body){
        if(!error && response.statusCode == 200){
            info = JSON.parse(body);
            info = info.localities.locality;
            if( Object.prototype.toString.call( info ) == '[object Array]' ){

                for ( var x = 0 ; x < info.length ; x++ ){
                    var locx = info[x].location;
                    var qsuburb = req.query.suburb;

                    if( locx == qsuburb.toUpperCase() ){
                        res.send( { 'postcode': info[x].postcode } );
                    }
                }

            } else if (Object.prototype.toString.call( info ) != '[object Array]') {

                var locx = info.location;
                var qsuburb = req.query.suburb;
                if ( locx == qsuburb.toUpperCase() ){
                    res.send( { 'postcode': info.postcode } );
                }
            }            
        }
    }
});

So, I am trying to request data from an API and then based on that data, I am then sending some data back. My code is as above.

Unfortunately, in the callback function, when I am running a loop to find a specific element that I will then send back to the client, when sending that element to client, I am getting the error as shown in the title.

This does not occur when there is no loop and I simply send one element of the data back.

Any ideas on what this could be?

Mac Ahmed
  • 55
  • 10
  • Once the response is sent for a particular request, again another response cannot be sent. So, try to accumulate the response and send it at once – Chandan Kumar V Feb 22 '18 at 06:55
  • I agree with @ChandanKumarV you need to keep the data you want to send maybe in an array and then send all at once. – Don Feb 22 '18 at 07:01
  • 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) – Rahul Feb 22 '18 at 07:31

3 Answers3

1

You are reaching the res.send function call at least twice in your flow and this is not allowed. That function must be called once to send the response to the client.

whites11
  • 12,008
  • 3
  • 36
  • 53
  • Ok. You mean in the loop, correct? I was under the assumption that res.send() would result in the program basically terminating at that stage, which I guess is clearly not true. – Mac Ahmed Feb 22 '18 at 07:18
  • exactly, res.send is not terminal, it simply sends back a response to the client. – whites11 Feb 22 '18 at 07:52
1

You are sending response more than one for one request, write res.send or JSON outside the loop.

for (var x = 0; x < info.length; x++) {
    var locx = info[x].location;
    var qsuburb = req.query.suburb;
    var postcodes = [];
    if (locx == qsuburb.toUpperCase()) {
        postcodes.push({
            'postcode': info[x].postcode
        });
    }
    res.json(postcodes);
}
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
0

Yeah, As @Rahul said, It is not allowed to send response more than one time for the same request - You must be getting same post code, so you either need to store your postcode in a variable and send it after loop overs or you can use a break. Though it is not advisable to use a break in complex loops in the pretty simple loop you can leverage its usage.

for (var x = 0; x < info.length; x++) {
    var locx = info[x].location;
    var qsuburb = req.query.suburb;

    if (locx == qsuburb.toUpperCase()) {
        res.send({
            'postcode': info[x].postcode
        });
        break;
    }
}

Or as following:

for (var x = 0; x < info.length; x++) {
    var locx = info[x].location;
    var qsuburb = req.query.suburb;
    vat postcode = = null;
    if (locx == qsuburb.toUpperCase()) {
        postcode = info[x].postcode
    }
}
res.send({
    postcode
});
Ridham Tarpara
  • 5,970
  • 4
  • 19
  • 39