Node.js JSON编码 - 数组输出为空
问题描述:
我使用这段代码来获取数据库列中的值,并使用JSON数组输出。我成功地在浏览器控制台上获得它,所以我用另一个值尝试了它,并使用相同的格式将代码从我的类文件传递到另一个文件上的路由器app.post。当我使用console.log
时,终端上可以看到它,但在浏览器响应中无法看到输出,所以出现了什么问题?Node.js JSON编码 - 数组输出为空
,成功地打印输出的代码:
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"
我试着写这个代码用PHP 发布GETCOLOR像
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(req.user.id,function(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));
});
编辑:在getColor
和getLastMove
功能,您要高度重视不参选回报:只要使用回调:因为connection.query
是异步的任何回报将是无效的;在getColor
你正在做 var col=""; return callback(col);
所以你将永远有一个“”响应。
当心:当它结束到服务器响应(您不能发送多个响应的一个请求)不调用回调函数几次:你的回调电话是在一个循环中,他们不应该,只是称之为一次。
忽略通过异步回调返回的错误是_never_不错的主意。 –
@partycoder谢谢,但没有任何不同仍输出空 –