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