因此,我对完全堆栈非常陌生,并且一直在做一个小的普通js项目,以创建一个网站来调用我运行的服务器,该服务器名为chess.com api,例如检索所有elo评级是:
因此,我在节点-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));
}
};然后,代码在main.js上使用express运行,其中:
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 (专业球员)工作,原子炖(朋友)不工作,idobl (朋友)不工作。最重要的是,它没有捕捉到任何错误,只是等待永远得不到的答案。但是,如果使用api,则它们都存在:
https://api.chess.com/pub/player/yoniramot/stats --我的https://api.chess.com/pub/player/hikaru/stats --支持https://api.chess.com/pub/player/atomicstew/stats --朋友https://api.chess.com/pub/player/idobl/stats --朋友
后端的控制台显示:
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中找不到它。
,请帮帮我,我的头脑在这一点上发狂了。
-edit 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)我打电话给你的推荐人就在那里,所以我还是被困住了。
-edit 2-
我没有意识到,如果玩家没有玩某种游戏模式,而不是api,那么这个值就不存在了。如果没有整洁的数据,如何将数据保存为0或null,有什么想法吗?
发布于 2021-02-01 08:55:33
在下面的代码块中,您试图访问不同属性上的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,
})https://stackoverflow.com/questions/65989351
复制相似问题