Sunday, 14 May 2017

node.js json encode array output are emptyps

I writed this code to get value from a database column to output with json array and i success to get that on the browser console response so i tried with other value and used the same format to the code to passing it from my class file to the router app.post on other file and i can see it on terminal when i using console.log but i can't see the output on the browser response so what's the wrong ?

the successfully outputing code

auth.js router part

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



});

the function that I call on move.js file

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;


}

the response on the browser console successfully outputting

msg:"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" 
loggedin:"true"

the outputting code that have the problem

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



});

the function that I call from the other file

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


}   

the value that I get on my browser console response output just

loggedin:"true"

that should be like that

msg:"white"
loggedin:"true"

I tried to write this code with php to post getcolor like that

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

and the function that i call

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

and the output was that on browser console responses

msg:"white"
loggedin:"true" 



via dark night

No comments:

Post a Comment