0

I get this error massage

Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)

after I used res.json on my code

app.post('/dispalymove', function (req, res, next) {

var lMove="";


if(req.body.MoveString !== null){
     Move.setMoveUserId(req.user.id);
     Move.setMoveString(req.body.MoveString);
     a.getLastMove(req.user.GameId,function(move){
      console.log("Return from display move:",move)
        res.json({"msg": move, "loggedin": "true"});


      });


    } else {

              var output = {"msg": lMove, "loggedin": "true"}
               res.json(output);
    }

});

the function that I called in another file move.js

getLastMove(id,callback){


    var MoveRequest = "SELECT * FROM users ORDER BY id";

    var query = connection.query(MoveRequest, function(err,rows, result) {

    if (rows.length == 0) { 
        return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
    }
    if (rows.length > 0) {
        for (var i in rows) {

            var move = rows[i].MoveString; 
            if (rows[i].GameId == id){

                callback(move);
            }

        }
    }


    });


        var move="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
        return move;


}

UPDATE I tried but a return after res,json like the example in the duplicated link and tried to comment the other app.post where i callback move but i still get this error i get the output in the first time the page load then server give me this error so that's not mean the request being left "dangling" so what's the wrong ?

the other app.post where I call "move" to

app.post('/insertmove', function (req, res, next) {

var lMove="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";


if(req.body.MoveString !== null){
     Move.setMoveUserId(req.user.id) ;
     Move.setMoveString(req.body.MoveString);
     a.InsertMove(req.user.GameId);
     a.getLastMove(req.user.GameId,function(move){
     console.log("Return from insertmove move:",move)
           var output = {"msg": move, "loggedin": "true"}; 

           return res.send(JSON.stringify(output));

      });


    } else {


             var output = {"msg": lMove, "loggedin": "true"}; // <=== here lCol always equals ""

             return res.send(JSON.stringify(output));

    }


});
dark night
  • 171
  • 4
  • 19
  • 1
    Possible duplicate of [Error: Can't set headers after they are sent to the client](http://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Obsidian Age May 14 '17 at 21:18
  • This isn't a complete example; you'll get dinged on that. Basically, res.json may set headers so you need to be sure that nothing prior to this sends any data to the response object. It would appear that getLastMove could call the callback more than once; the 2nd time it calls it it would trigger this kind of error. – Joe May 14 '17 at 21:24
  • @Joe i have another app.post function that callback getLastMove are that the problem ? – dark night May 14 '17 at 21:26
  • Also: your `return move;` at the end of `getLastMove` isn't going to do anything. You're not using the return value in your calling code, you're expecting the callback to get handed that info. I'd expect that return to instead be `callback(move);` – Joe May 14 '17 at 21:26
  • For any given request, you can only send one response. You should make sure `getLastMove` does what you think it does, especially in that loop. You're assuming I think that it only finds one matching row. – Joe May 14 '17 at 21:27
  • There are also cases when getLastMove will not call the callback at all, which will lead to the request being left "dangling" – jcaron May 14 '17 at 21:35
  • I tried but a return after res,json like the example in the duplicated link and tried to comment the other app.post where i callback move but i still get this error i get the output in the first time the page load then server give me this error so that's not mean the request being left "dangling" so what's the wrong ? – dark night May 14 '17 at 21:42
  • @jcaron i updated my post can you check that please – dark night May 14 '17 at 21:47
  • @Joe i tried to remove the other app.post where i call it again but did't work and check my new post update i added the other app.post code – dark night May 14 '17 at 21:49
  • As pointed out, the issue is in your getLastMove function. You need to make sure it always calls the callback once, never more, never less. Currently it can call it 0, 1 or more times. Whenever it doesn't call it or calls it several times you'll have an issue. – jcaron May 14 '17 at 21:51
  • You can only send a response once. If you send it more than once you will get this error. – arboreal84 May 14 '17 at 21:55

1 Answers1

0

I fixed this error by adding return after callback

    var move = rows[i].MoveString; 
    if (rows[i].GameId == id){

        callback(move);
    }

like that

    var move = rows[i].MoveString; 
    if (rows[i].GameId == id){

        callback(move);
        return;
    }
dark night
  • 171
  • 4
  • 19