我想这个代码从数据库列中获取一个值,并在JSON数组中输出。我成功地在浏览器控制台上获取它,所以我尝试使用其他值并使用相同的格式将代码从我的类文件传递到另一个文件上的路由器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"
我试着用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"
答案 0 :(得分:0)
这是因为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
功能中,你不返回anthing:只需使用回调:因为connection.query
是异步的,任何返回都将无效;在getColor
你正在做
var col="";
return callback(col);
,所以你总会有一个&#34;&#34;响应。
注意:当它结束服务器响应时,不要多次调用回调函数(你不能为一个请求发送多个响应):你的回调调用是循环的,他们不应该,只需调用一次。