我正在尝试响应 http 请求,但收到以下错误:
TypeError: req.json is not a function
这是我称之为的地方:
app.get('/', function (req, res) {
const channel_name = req.query.channel_name;
const user_name = req.query.user_name;
const text = req.query.text;
var input = text.split(" ");
if (input[0] == "move") { // Make move
game.move(user_name, channel_name, input[1], input[2], function(returnGame) {
req.json(game.getGameStatus(returnGame));
});
}
else {
var boardSize = 3;
if (input.length == 2)
boardSize = input[1];
var newGame = game.startGame(channel_name, user_name, input[0], boardSize);
res.json(game.getGameStatus(newGame));
}
});
app.listen(port);
game.move 和 game.startGame 都返回一个名为 Game 的 JSON。 StartGame 创建 JSON 并返回它,但 move 从数据库加载 JSON 并使用回调返回它。 当我从回调中调用 req.json 时收到错误,但它对于 StartGame 工作正常。
有什么想法吗?我一整天都被困在这个问题上,似乎无法弄清楚。感谢任何帮助,谢谢。
最佳答案
在 req.json(game.getGameStatus(returnGame)); 处检测到输入错误
.json
是为 res
定义的,而不是为 req
定义的。
关于Node.js - Express - 类型错误 : res. json 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300868/