chess.com API 没有带回一些请求
chess.com API does'nt bring back some requests
所以我对全栈很陌生,一直在做一个小的 vanilla js 项目来制作一个网站,该网站调用我 运行 调用 chess.com api 的服务器,例如检索所有 elo 评分为:
所以我在 node-js 中进行了后端调用:
const fetch = require('node-fetch');
exports.getChessManStats = () => {
return (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
const username = req.param('username');
console.log(req.param('username'));
fetch(`https://api.chess.com/pub/player/${username}/stats`)
.then(res2 => res2.json() )
.then(chessManData => {
res.json({
daily: chessManData.chess_daily.last.rating,
rapid: chessManData.chess_rapid.last.rating,
blitz: chessManData.chess_blitz.last.rating,
bullet: chessManData.chess_bullet.last.rating,
})
})
.catch(err => console.log(err));
}
};
代码然后 运行s with express on the main.js with:
const app = express();
app.get('/getRating',chessManController.getChessManStats());
显然后来我打电话给 app.listen(3000)
.
然后在前端我做了一个调用这个函数的按钮:
const makeUserStats = () => {
const username = document.getElementById('username').value;//id=username is type input
document.getElementById('rating').innerHTML = `Searching`; //making the rating div into 'searching'
console.log(`http://localhost:3000/getRating?username=${username}`)//just seeing the sting I send
fetch(`http://localhost:3000/getRating?username=${username}`)
.then(rating => {
document.getElementsByClassName('statButton').disabled = true;//disabling buttons until the data arrives
console.log('got json');
return rating.json();
})
.catch(err => console.log(err))//looking for errors
.then((userRating => {
window.userRating = userRating;//saving the stats globaly to use in other buttons
window.user = username;
}))
.then(() => {
document.getElementsByClassName('statButton').disabled = false;//enabling the buttons again
document.getElementById('rating').innerHTML = `${window.user} rating is:`;
})
}
现在,当我按下按钮时,它可以使用一些用户名和一些不使用的用户名。
YoniRamot(我的)有效,hikaru(职业玩家)有效,atomicstew(朋友)无效,idobl(朋友)无效。奇怪的是它不会捕获任何错误,只是等待它永远不会得到的答案。
但是如果你去 api 它们都存在:
https://api.chess.com/pub/player/hikaru/stats --proplayer
后台控制台显示:
atomicstew
TypeError: Cannot read property 'last' of undefined
at C:\Users\Yonatan\Desktop\coding\training\intermediateChess\backend\controllers\chessStats.js:12:45
at processTicksAndRejections (internal/process/task_queues.js:93:5)
意味着后端获取了用户名,但出于某种原因在 api 中找不到它。
请帮帮我,我的脑袋现在乱七八糟。
-编辑 1-
所以我在发送带有数据的 res 之前添加了一个控制台日志,它打印了:
atomicstew
{
chess_rapid: {
last: { rating: 1228, date: 1612114999, rd: 28 },
best: {
rating: 1265,
date: 1611786478,
game: 'https://www.chess.com/live/game/6380128206'
},
record: { win: 233, loss: 202, draw: 19 }
},
chess_blitz: {
last: { rating: 902, date: 1611928398, rd: 50 },
best: {
rating: 1010,
date: 1609882454,
game: 'https://www.chess.com/live/game/6297568401'
},
record: { win: 26, loss: 24, draw: 4 }
},
fide: 0,
tactics: {
highest: { rating: 1659, date: 1609635730 },
lowest: { rating: 387, date: 1608148134 }
},
lessons: {},
puzzle_rush: {}
}
TypeError: Cannot read property 'last' of undefined
at C:\Users\Yonatan\Desktop\coding\training\intermediateChess\backend\controllers\chessStats.js:13:45
at processTicksAndRejections (internal/process/task_queues.js:93:5)
我调用的引用就在那里,所以我仍然卡住了。
-编辑 2-
我没有意识到,如果玩家不玩某种游戏模式,那么 api 就不会持有该值。如果没有数据,如何将数据整齐地保存为 0 或 null,有什么想法吗?
问题出在以下代码块中,您在其中尝试访问不同属性的 last
字段作为响应:
res.json({
daily: chessManData.chess_daily.last.rating,
rapid: chessManData.chess_rapid.last.rating,
blitz: chessManData.chess_blitz.last.rating,
bullet: chessManData.chess_bullet.last.rating,
})
我快速查看了以下 API 的回复,但它没有 chess_daily
和 chess_bullet
属性。
https://api.chess.com/pub/player/atomicstew/stats
由于您正在尝试访问 chessManData.chess_daily.last.rating
并且 chess_daily
不存在于 chessManData
中,您将获得 .last
.
的异常
为了修复它,您可以通过以下方式替换上面的块:
res.json({
daily: chessManData.chess_daily && chessManData.chess_daily.last ? chessManData.chess_daily.last.rating : null, // replace null with appropriate default value for rating
rapid: chessManData.chess_rapid && chessManData.chess_rapid.last ? chessManData.chess_rapid.last.rating : null,
blitz: chessManData.chess_blitz && chessManData.chess_blitz.last ? chessManData.chess_blitz.last.rating : null,
bullet: chessManData.chess_bullet && chessManData.chess_bullet.last ? chessManData.chess_bullet.last.rating : null,
})
所以我对全栈很陌生,一直在做一个小的 vanilla js 项目来制作一个网站,该网站调用我 运行 调用 chess.com api 的服务器,例如检索所有 elo 评分为:
所以我在 node-js 中进行了后端调用:
const fetch = require('node-fetch');
exports.getChessManStats = () => {
return (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
const username = req.param('username');
console.log(req.param('username'));
fetch(`https://api.chess.com/pub/player/${username}/stats`)
.then(res2 => res2.json() )
.then(chessManData => {
res.json({
daily: chessManData.chess_daily.last.rating,
rapid: chessManData.chess_rapid.last.rating,
blitz: chessManData.chess_blitz.last.rating,
bullet: chessManData.chess_bullet.last.rating,
})
})
.catch(err => console.log(err));
}
};
代码然后 运行s with express on the main.js with:
const app = express();
app.get('/getRating',chessManController.getChessManStats());
显然后来我打电话给 app.listen(3000)
.
然后在前端我做了一个调用这个函数的按钮:
const makeUserStats = () => {
const username = document.getElementById('username').value;//id=username is type input
document.getElementById('rating').innerHTML = `Searching`; //making the rating div into 'searching'
console.log(`http://localhost:3000/getRating?username=${username}`)//just seeing the sting I send
fetch(`http://localhost:3000/getRating?username=${username}`)
.then(rating => {
document.getElementsByClassName('statButton').disabled = true;//disabling buttons until the data arrives
console.log('got json');
return rating.json();
})
.catch(err => console.log(err))//looking for errors
.then((userRating => {
window.userRating = userRating;//saving the stats globaly to use in other buttons
window.user = username;
}))
.then(() => {
document.getElementsByClassName('statButton').disabled = false;//enabling the buttons again
document.getElementById('rating').innerHTML = `${window.user} rating is:`;
})
}
现在,当我按下按钮时,它可以使用一些用户名和一些不使用的用户名。 YoniRamot(我的)有效,hikaru(职业玩家)有效,atomicstew(朋友)无效,idobl(朋友)无效。奇怪的是它不会捕获任何错误,只是等待它永远不会得到的答案。 但是如果你去 api 它们都存在:
https://api.chess.com/pub/player/hikaru/stats --proplayer
后台控制台显示:
atomicstew
TypeError: Cannot read property 'last' of undefined
at C:\Users\Yonatan\Desktop\coding\training\intermediateChess\backend\controllers\chessStats.js:12:45
at processTicksAndRejections (internal/process/task_queues.js:93:5)
意味着后端获取了用户名,但出于某种原因在 api 中找不到它。
请帮帮我,我的脑袋现在乱七八糟。
-编辑 1-
所以我在发送带有数据的 res 之前添加了一个控制台日志,它打印了:
atomicstew
{
chess_rapid: {
last: { rating: 1228, date: 1612114999, rd: 28 },
best: {
rating: 1265,
date: 1611786478,
game: 'https://www.chess.com/live/game/6380128206'
},
record: { win: 233, loss: 202, draw: 19 }
},
chess_blitz: {
last: { rating: 902, date: 1611928398, rd: 50 },
best: {
rating: 1010,
date: 1609882454,
game: 'https://www.chess.com/live/game/6297568401'
},
record: { win: 26, loss: 24, draw: 4 }
},
fide: 0,
tactics: {
highest: { rating: 1659, date: 1609635730 },
lowest: { rating: 387, date: 1608148134 }
},
lessons: {},
puzzle_rush: {}
}
TypeError: Cannot read property 'last' of undefined
at C:\Users\Yonatan\Desktop\coding\training\intermediateChess\backend\controllers\chessStats.js:13:45
at processTicksAndRejections (internal/process/task_queues.js:93:5)
我调用的引用就在那里,所以我仍然卡住了。
-编辑 2-
我没有意识到,如果玩家不玩某种游戏模式,那么 api 就不会持有该值。如果没有数据,如何将数据整齐地保存为 0 或 null,有什么想法吗?
问题出在以下代码块中,您在其中尝试访问不同属性的 last
字段作为响应:
res.json({
daily: chessManData.chess_daily.last.rating,
rapid: chessManData.chess_rapid.last.rating,
blitz: chessManData.chess_blitz.last.rating,
bullet: chessManData.chess_bullet.last.rating,
})
我快速查看了以下 API 的回复,但它没有 chess_daily
和 chess_bullet
属性。
https://api.chess.com/pub/player/atomicstew/stats
由于您正在尝试访问 chessManData.chess_daily.last.rating
并且 chess_daily
不存在于 chessManData
中,您将获得 .last
.
为了修复它,您可以通过以下方式替换上面的块:
res.json({
daily: chessManData.chess_daily && chessManData.chess_daily.last ? chessManData.chess_daily.last.rating : null, // replace null with appropriate default value for rating
rapid: chessManData.chess_rapid && chessManData.chess_rapid.last ? chessManData.chess_rapid.last.rating : null,
blitz: chessManData.chess_blitz && chessManData.chess_blitz.last ? chessManData.chess_blitz.last.rating : null,
bullet: chessManData.chess_bullet && chessManData.chess_bullet.last ? chessManData.chess_bullet.last.rating : null,
})