Home > Software engineering >  TypeError: cannot read property '0' of undefined nodejs request http
TypeError: cannot read property '0' of undefined nodejs request http

Time:10-23

I'm trying to extend my Discord Bot with different commands which retrieves a JSON from an API. It worked for the first few like:

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;

So that was working, nice. When I try to get other values from another API-call (Driver Standings for f1 seasons), I'm running into following logs on my 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)

My faulty code is following, where the logs say, that Standingslist(?) is undefined, even though it can be seen in the http response? Faulty code:

                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;

Syntax-wise, I just can't figure out my error.

The JSON is structured as following: json structure screenshot

API for json is: JSON

CodePudding user response:

i didnt tried you code, bit it seems it should be Standingslists and not Standingslist. Can you try it?

Br Patrick

CodePudding user response:

typo -> StandingList instead of StandingsList

  • Related