TypeError: cannot read property '0' of undefined nodejs request http

TypeError: cannot read property '0' of undefined nodejs request http

我正在尝试使用不同的命令扩展我的 Discord Bot,这些命令从 API 中检索 JSON。它适用于前几个,例如:

case 'results':
                request('http://ergast.com/api/f1/'+args[2]+'/'+args[3]+'/results.json', (error, response, body) => {
                    if(error) {msg.reply(error); }
                    const temp = JSON.parse(body);
                    let answer = '';
                    try {
                        for(var i = 0; i < temp.MRData.RaceTable.Races[0].Results.length; i++) {
                            answer += (i+1)+'. ' 
                                + temp.MRData.RaceTable.Races[0].Results[i].Driver.givenName +' '
                                + temp.MRData.RaceTable.Races[0].Results[i].Driver.familyName + ', ' 
                                + temp.MRData.RaceTable.Races[0].Results[i].Constructor.name + '\n';
                        }
                    }catch(error) {
                        console.log(error);
                        answer = 'Choose earlier race, no results in yet';
                    }   
                    msg.reply(answer);
                });
                break;

这很有效,很好。当我尝试从另一个 API-call(f1 赛季的车手排名)获取其他值时,我 运行 进入我的 Heroku Deployed Discordbot 上的以下日志:

2021-10-22T13:35:57.470709+00:00 app[worker.1]: TypeError: Cannot read property '0' of undefined
    2021-10-22T13:35:57.470710+00:00 app[worker.1]:     at Request._callback (/app/index.js:254:73)
    2021-10-22T13:35:57.470710+00:00 app[worker.1]:     at Request.self.callback (/app/node_modules/request/request.js:185:22)
    2021-10-22T13:35:57.470710+00:00 app[worker.1]:     at Request.emit (events.js:400:28)
    2021-10-22T13:35:57.470711+00:00 app[worker.1]:     at Request.<anonymous> (/app/node_modules/request/request.js:1161:10)
    2021-10-22T13:35:57.470711+00:00 app[worker.1]:     at Request.emit (events.js:400:28)
    2021-10-22T13:35:57.470711+00:00 app[worker.1]:     at IncomingMessage.<anonymous> (/app/node_modules/request/request.js:1083:12)
        2021-10-22T13:35:57.470712+00:00 app[worker.1]:     at Object.onceWrapper (events.js:519:28)
        2021-10-22T13:35:57.470712+00:00 app[worker.1]:     at IncomingMessage.emit (events.js:412:35)
        2021-10-22T13:35:57.470712+00:00 app[worker.1]:     at endReadableNT 
(internal/streams/readable.js:1334:12)
        2021-10-22T13:35:57.470713+00:00 app[worker.1]:     at processTicksAndRejections 

(internal/process/task_queues.js:82:21)

我的错误代码如下,日志显示 Standingslist(?) 未定义,即使它可以在 http 响应中看到? 错误代码:

                case 'driverstandings':                 
                    request('http://ergast.com/api/f1/'+args[2]+'/driverStandings.json', (error, response, body) => {
                    if(error) {msg.reply(error); }
                    const temp = JSON.parse(body);
                    let answer = 'Season: ' + temp.MRData.StandingsTable.StandingsList[0].round;
                    try {
                        for(var i = 0; i < temp.MRData.StandingsTable.StandingsList[0].DriverStandings.length; i++) {
                            answer += (i+1)+'. ' 
                                + temp.MRData.StandingsTable.StandingsList[0].DriverStandings[i].Driver.givenName +' '
                                + temp.MRData.StandingsTable.StandingsList[0].DriverStandings[i].Driver.familyName + ', ' 
                                + temp.MRData.StandingsTable.StandingsList[0].DriverStandings[i].points +' points, wins: '
                                + temp.MRData.StandingsTable.StandingsList[0].DriverStandings[i].wins +'\n';
                        }
                    }catch(error) {
                        console.log(error);
                        answer = 'No entries';
                    }   
                    msg.reply(answer);
                });
                break;

语法方面,我就是想不出我的错误。

JSON 的结构如下:json structure screenshot

API 对于 json 是:JSON

我没有试过你的代码,但它似乎应该是 Standingslists 而不是 Standingslist。 可以试试吗?

Br 帕特里克

打字错误 -> StandingList 而不是 StandingsList