TypeError: Cannot read property ‘0’ of undefined Google Spreadsheets API

Solution for TypeError: Cannot read property ‘0’ of undefined Google Spreadsheets API
is Given Below:

I’m trying to fetch players list from Wargaming API to Google Spreadsheet. This is the code:

function playersList() {
    const sheet = SpreadsheetApp.getActiveSheet();

    const initUrl = UrlFetchApp.fetch(
        'https://api.worldoftanks.eu/wot/globalmap/eventrating/?application_id=e631b3284e60a8d5b589545bbaa6b3af&front_id=thunderstorm_bg&event_id=thunderstorm&limit=100&page_no=1',
    );
    const initJson = initUrl.getContentText();
    var initData = JSON.parse(initJson);
    const numOfSites = initData['meta']['page_total'];

    const urls = [];

    for (var i = 1; i < numOfSites; i++) {
        let url = `https://api.worldoftanks.eu/wot/globalmap/eventrating/?application_id=e631b3284e60a8d5b589545bbaa6b3af&front_id=thunderstorm_bg&event_id=thunderstorm&limit=100&page_no=${i}`;
        urls.push(url);
    }

    var data = [];

    for (var x = 0; x < numOfSites - 1; x++) {
        var loopUrl = UrlFetchApp.fetch(urls[x]);
        var loopJson = loopUrl.getContentText();
        var DATA = JSON.parse(loopJson);
        data.push(DATA);
    }

    for (var z = 0; z < numOfSites - 1; z++) {
        for (var y = 0; y < 99; y++) {
            sheet
                .getRange(sheet.getLastRow() + 1, 1)
                .setValue([`data${i}`['data'][y]['rank']]);
        }
    }
}

Apparently an issue is .setValue([data${i}['data'][y]['rank']]);
Error returned is: TypeError: Cannot read property ‘0’ of undefined

Let’s break your .setValue down:

[`data${i}`['data'][y]['rank']]
// with some formatting and brackets addded:
[ (`data${i}`)['data'][y]['rank'] ]
// split in statements
const str = `data${i}`;
[ str['data'][y]['rank']

Your data${i} is a string, and strings don’t have a .data field, thus resulting in undefined. Then you perform [y] where y is 0 during the first loop, and that’s why you get that error.

I assume you meant to do data[i] instead of

`data${i}`