Sunday, 14 May 2017

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

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;


}



via dark night

No comments:

Post a Comment