Node.JSON JSON Encode–;数组输出为空

Node.JSON JSON Encode–;数组输出为空,json,node.js,Json,Node.js,我编写这段代码是为了从数据库列中获取一个值,以便在JSON数组中输出。我成功地在浏览器控制台上获取了它,因此我尝试使用另一个值,并使用相同的代码格式将其从类文件传递到另一个文件上的router app.post。当我使用console.log时,我可以在终端上看到它,但是在浏览器响应中我看不到输出,所以怎么了 成功打印输出的代码: auth.js,路由器部分 app.post('/dispalymove', function (req, res, next) { var lMove="";

我编写这段代码是为了从数据库列中获取一个值,以便在JSON数组中输出。我成功地在浏览器控制台上获取了它,因此我尝试使用另一个值,并使用相同的代码格式将其从类文件传递到另一个文件上的router app.post。当我使用
console.log时,我可以在终端上看到它,但是在浏览器响应中我看不到输出,所以怎么了

成功打印输出的代码:

auth.js
,路由器部分

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

var lMove="";


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

      });


    }
     var output = {"msg":lMove, "loggedin":"true"};

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



});
我在
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 callback ("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;


}
输出成功时浏览器控制台上的响应:

msg:"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" 
loggedin:"true"
有问题的输出代码

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

var lCol="";

if(req.body.MoveString !== null){
     Move.setMoveUserId(req.user.id);
     lCol = a.getColor(req.user.id,function(col){
     console.log("Return from getcolor:",col)
             //the the value that i get on terminal "Return from getcolor:white"

     });
    }

     var output = {"msg":lCol, "loggedin":"true"};
     res.send(JSON.stringify(output));
});
我从另一个文件调用的函数:

getColor(id,callback){

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

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

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

            var col = rows[i].GameColor;

            if (rows[i].id == id){

                 callback(col);
                }

        }
    }
    });

        var col="";
        return callback(col);
}   
我在浏览器控制台响应输出上获得的值

loggedin:"true"
应该是这样的

msg:"white"
loggedin:"true"
session_start();
include "../classes/move.php";
$lCol="";
if(isset($_POST['MoveString'])){
    $move = new move();
    $move->setMoveUserId($_SESSION['UserId']);
    $lCol=$move->getColor($_SESSION['UserId']);
}  
$output = array("msg"=>"$lCol", "loggedin"=>"true");
echo json_encode($output);
我试图用php编写这段代码 像那样发布getcolor

msg:"white"
loggedin:"true"
session_start();
include "../classes/move.php";
$lCol="";
if(isset($_POST['MoveString'])){
    $move = new move();
    $move->setMoveUserId($_SESSION['UserId']);
    $lCol=$move->getColor($_SESSION['UserId']);
}  
$output = array("msg"=>"$lCol", "loggedin"=>"true");
echo json_encode($output);
还有我调用的函数

public function getColor($id){
    include "../../connectToDB.php";

    $ColRequest=$_db->query("SELECT * FROM users ORDER BY UserId");
    $existCount = $ColRequest->rowCount();

    if ($existCount == 0) { // evaluate the count
        return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
    }
    if ($existCount > 0) {
        while($row = $ColRequest->fetch(PDO::FETCH_ASSOC)){
            $userID = $row["UserId"];               
            $col = $row["GameColor"];                   
            if($userID == $id) {
                return $col;

            }
        }
    }
    $col="";
    return $col;
}
输出是浏览器控制台上的响应

msg:"white"
loggedin:"true" 

这是因为
lCol=a.getColor(请求用户id,函数(col)

lCol
在此处未定义,因为
getColor
不返回任何内容,它需要回调并在此回调中为您提供一个值(相反,
getLastMove
会返回一些内容)。请尝试:

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

    var lCol = "";

    if (req.body.MoveString !== null) {
        Move.setMoveUserId(req.user.id);
        // lCol = <=== useless because a.getcolor returns nothing
        a.getColor(req.user.id, function (col) {
            console.log("Return from getcolor:", col)
            //the the value that i get on terminal "Return from getcolor:white"
            res.json({"msg": col, "loggedin": "true"}); // <=== here you have a defined lCol
        });
    } else {
        var output = {"msg": lCol, "loggedin": "true"}; // <=== here lCol always equals ""
        res.json(output);
    }

    // var output = {"msg": lCol, "loggedin": "true"}; // <=== here lCol always equals undefined (when req.body.MoveString !== null)
    // res.send(JSON.stringify(output));

});
app.post('/getcolor',函数(req,res,next){
var lCol=“”;
if(req.body.MoveString!==null){
Move.setMoveUserId(req.user.id);

//lCol=忽略通过异步回调返回的错误从来都不是一个好主意。@partycoder谢谢,但这并没有使任何不同的输出仍然为空