thejosejorge / futcal-for-scriptable

Futcal is a football widget for Scriptable.
76 stars 25 forks source link

TypeError: undefined is not an object 'teamData.table[0].tables.length' #41

Open beratgashi opened 1 year ago

beratgashi commented 1 year ago

I think FotMob again changed something in the structure of the API but i can't manage to fix it, would be really helpful if anyone tells us in which line to change what!

Thanks and Regards.

hangoon-p commented 1 year ago

The reason why this script doesn't work now is since Fotmob API return value - the JSON structure is changed. Basically teamData.table[0] need to be replaced to teamData.table[0].data, but it is not simple like that because there are other changes in detail. I organized the changes as below, so I guess with this you can change your code by yourself.

Line 95 const leagueOverviewUrl = encodeURI(`${baseApiUrl}${teamData.table[0].pageUrl}`); replace to const leagueOverviewUrl = encodeURI(`${baseApiUrl}${teamData.table[0].data.pageUrl}`);

Line 337 - 359

  if(teamData.table) {
    let isSingleTable = teamData.table[0].table;
    let leagueTable;
    let leagueTitle = teamData.table[0].leagueName;
    let leagueSubtitle;
    // If league table is not found assume it is a special case with more than one table available
    if (isSingleTable) {
      leagueTable = teamData.table[0].table.all;
    }
    else {
        let teamFound;
        let tableIndex = 0;
        for (let i = 0; i < teamData.table[0].tables.length; i += 1) {
            teamFound = (teamData.table[0].tables[i].table.all).findIndex(obj => obj.id == teamData.details.id);
            if (teamFound != -1) {
                tableIndex = i;
                break;
            }
        }
        leagueTable = teamData.table[0].tables[tableIndex].table.all;
        leagueSubtitle = teamData.table[0].tables[tableIndex].leagueName;
        leagueSubtitle = leagueSubtitle.startsWith("- ") ? leagueSubtitle.substring(2) : leagueSubtitle;
    }

replace to

  if(teamData.table) {
    let isSingleTable = teamData.table.length;
    let leagueTable;
    let leagueTitle = teamData.table[0].data.leagueName;
    let leagueSubtitle;
    // If league table is not found assume it is a special case with more than one table available
    if (isSingleTable == 1) {
      leagueTable = teamData.table[0].data.table.all;
    }
    else {
        let teamFound;
        let tableIndex = 0;     
        for (let i = 0; i < teamData.table.length; i += 1) {
            teamFound = (teamData.table[i].data.table.all).findIndex(obj => obj.id == teamData.details.id);
            if (teamFound != -1) {
                tableIndex = i;
                break;
            }
        }
        leagueTable = teamData.table[tableIndex].data.table.all;
        leagueSubtitle = teamData.table[tableIndex].data.leagueName;
        leagueSubtitle = leagueSubtitle.startsWith("- ") ? leagueSubtitle.substring(2) : leagueSubtitle;
    }

I don't fully understand Jorge's method because I don't have the return value before changed. What I did is fixing this script to seem working again. So it seems working now, but I am not quite sure it is fixed as it was intended. We might need to wait for Jorge's update.

beratgashi commented 1 year ago

@hangoon-p thank you bro, it worked out. At least for now it's a great fix until Jorge releases a new version.

Thanks, i really appreciate it!

beratgashi commented 1 year ago

@hangoon-p how are you doing bro, did you found any solution for the newest error "No data" ?

epifinygirl commented 1 year ago

@hangoon-p I’m getting an error after these updates you posted “undefined is not an object (evaluating 'leagueTable.findIndex')”. This widget works for NWSL data but I’m trying to get MLS data and it’s giving me an error. Would you be able to help out?

hangoon-p commented 1 year ago

@epifinygirl

It is because MLS has 2 sub-leagues, and the JSON structure for League Table is different than others. (which has no sub league) I only watch EPL so I didn't catch this error.

Now I fix the whole function. (not sure I did it right though)

Find the code below it is the same code with above fix.

  if(teamData.table) {
    let isSingleTable = teamData.table.length;
    let leagueTable;
    let leagueTitle = teamData.table[0].data.leagueName;
    let leagueSubtitle;
    // If league table is not found assume it is a special case with more than one table available
    if (isSingleTable == 1) {
      leagueTable = teamData.table[0].data.table.all;
    }
    else {
        let teamFound;
        let tableIndex = 0;     
        for (let i = 0; i < teamData.table.length; i += 1) {
            teamFound = (teamData.table[i].data.table.all).findIndex(obj => obj.id == teamData.details.id);
            if (teamFound != -1) {
                tableIndex = i;
                break;
            }
        }
        leagueTable = teamData.table[tableIndex].data.table.all;
        leagueSubtitle = teamData.table[tableIndex].data.leagueName;
        leagueSubtitle = leagueSubtitle.startsWith("- ") ? leagueSubtitle.substring(2) : leagueSubtitle;
    }

replace it to

  if(teamData.table) {
    let isSingleTable = teamData.table[0].data.composite;
    let leagueTable;
    let leagueTitle = teamData.table[0].data.leagueName;
    let leagueSubtitle;
    // If league table is not found assume it is a special case with more than one table available
    if (isSingleTable == false) {
      leagueTable = teamData.table[0].data.table.all;      
    }
    else {
        let teamFound;
        let tableIndex = 0;     
        for (let i = 0; i < teamData.table[0].data.tables.length; i += 1) {
            teamFound = (teamData.table[0].data.tables[i].table.all).findIndex(obj => obj.id == teamData.details.id);
            if (teamFound != -1) {
                tableIndex = i;
                break;
            }
        }       
        leagueTable = teamData.table[0].data.tables[tableIndex].table.all;
        leagueSubtitle = teamData.table[0].data.tables[tableIndex].leagueName;
        leagueSubtitle = leagueSubtitle.startsWith("- ") ? leagueSubtitle.substring(2) : leagueSubtitle;
    }

I hope nothing wrong with this :)