array[0] returns undefined even if the following array contains an object with data in it (fixed)

Viewed 36

What I had to do was to put a check on the ParseCompiti() function: if(compito.compiti.length == 0) continue;

export function parseCompiti(datajson: any) {
    let obj: Record<string, any> = {}
    const json = datajson
    for(let compito of json.registro) {
        if(compito.compiti.length == 0) continue;
        
        if(toUnix(JSON.stringify((compito.compiti[0]).dataConsegna)) >= toUnix(handleDate(Date.now()).postDate)) {
            const materia = compito.materia.trim()
            if(!obj[materia]) {
                obj[materia] = []
            }
           if(compito.compiti.length) { 
                    obj[materia].push({
                    data: (compito.compiti[0]).dataConsegna,
                    compito: (compito.compiti[0]).compito,
                })
            }
        }
    }
    return obj
}

I've been trying to make a bot that scrapes the homework's information and gets it on a single object, but I ran into an error.

This is the object:

  {
    datEvento: '2022-10-04 00:00:00',
    compiti: [ [Object] ],
    datGiorno: '2022-10-04',
    materia: 'ITALIANO  ',  
    attivita: null,
    ora: 4
  }

for context, this is the compiti array:

            "compiti": [
              {
                "compito": "Learn from page 10 to 20 (just for example)", 
                "dataConsegna": "2022-09-21"
              }
            ]

compito is what I have to do, while dataConsegna is the day I need to give it

What I'm trying to get is the "compiti" array so normally i would try obj.compiti[0] and get the information, I've also tried to parse it with JSON.stringify(obj.compiti[0]), but, if I try to get something from that object, it will always return undefined, how can I fix this?

Full code:

export async function getDati() {
    const now = handleDate(Date.now())
    let update_data = `{\r\n    "dataultimoaggiornamento": "${now.postDate} ${now.time}.002524"\r\n}`;
 
    let update_config = {
        method: 'post',
        url: 'https://www.portaleargo.it/appfamiglia/api/rest/dashboard/aggiornadata',
        headers: { 
            'accept': 'application/json', 
            'accept-encoding': 'gzip', 
            'Authorization': 'Bearer ' + global.bearer, 
            'content-type': 'application/json; charset=utf-8', 
            'os-type': 'android SQ3A.220705.004', 
            'user-agent': 'Dart/2.14 (dart:io)', 
            'x-auth-token': global.token, 
            'x-cod-min': 'SM13219'
        },
        data : update_data
    };
 
    await axios(update_config)
    var data = `{\r\n  "dataultimoaggiornamento": "${now.postDate} ${now.time}",\r\n  "opzioni": "{\\"ORARIO_SCOLASTICO\\":true,\\"PAGELLINO_ONLINE\\":true,\\"VALUTAZIONI_PERIODICHE\\":true,\\"VALUTAZIONI_GIORNALIERE\\":true,\\"COMPITI_ASSEGNATI\\":true,\\"IGNORA_OPZIONE_VOTI_DOCENTI\\":false,\\"DOCENTI_CLASSE\\":true,\\"RENDI_VISIBILE_CURRICULUM\\":true,\\"RICHIESTA_CERTIFICATI\\":false,\\"MODIFICA_RECAPITI\\":true,\\"CONSIGLIO_DI_ISTITUTO\\":true,\\"NOTE_DISCIPLINARI\\":true,\\"GIUDIZI\\":false,\\"GIUSTIFICAZIONI_ASSENZE\\":true,\\"TABELLONE_PERIODI_INTERMEDI\\":false,\\"PAGELLE_ONLINE\\":true,\\"ASSENZE_PER_DATA\\":true,\\"ARGOMENTI_LEZIONE\\":false,\\"NASCONDI_DIDUP_FAMIGLIA\\":true,\\"ALILITA_BSMART_FAMIGLIA\\":false,\\"VOTI_GIUDIZI\\":true,\\"ABILITA_AUTOCERTIFICAZIONE_FAM\\":false,\\"TABELLONE_SCRUTINIO_FINALE\\":false,\\"PIN_VOTI\\":false,\\"DISABILITA_ACCESSO_FAMIGLIA\\":true,\\"TASSE_SCOLASTICHE\\":true,\\"PROMEMORIA_CLASSE\\":true,\\"PRENOTAZIONE_ALUNNI\\":true,\\"CONSIGLIO_DI_CLASSE\\":true}"\r\n}`;
 
    var config = {
        method: 'post',
        url: 'https://www.portaleargo.it/appfamiglia/api/rest/dashboard/dashboard',
        headers: { 
            'accept': 'application/json', 
            'accept-encoding': 'gzip', 
            'argo-client-version': '1.9.1', 
            'authorization': 'Bearer ' + global.bearer,
            'content-type': 'application/json; charset=utf-8', 
            'os-type': 'android SQ3A.220705.004', 
            'user-agent': 'Dart/2.14 (dart:io)', 
            'x-auth-token': global.token,
            'x-cod-min': 'SM13219'
        },
        data : data
    };
 
 
    const res = await axios(config)
    const workeddata = res.data.data.dati[0]
    return workeddata;
}
 
export async function parseCompiti(datajson: any) {
    let obj: Record<string, any> = {}
    const json = datajson
    console.log(json.registro)
 
    for(let compito of json.registro) {
 
        if(toUnix(JSON.stringify(compito.compiti[0]).dataConsegna) >= toUnix(handleDate(Date.now()).postDate)) {
            const materia = compito.materia.trim()
            if(!obj[materia]) {
                obj[materia] = []
            }
           if(compito.compiti.length) { 
                    obj[materia].push({
                    data: (compito.compiti[0]).dataConsegna,
                    compito: (compito.compiti[0]).compito,
                })
            }
        }
    }
    return obj
}
// This will get called from index.ts:
client.on('message', async(msg) => {
    if (msg.body === '!compiti') {
        msg.reply('Downloading the content')
        await login() // logs into the online platform we use 
        const dati = await getDati()
        const compitidioggi = await parseCompiti(dati)
        const coso = JSON.stringify(compitidioggi, null, 4)
        msg.reply(coso)
        console.log(compitidioggi)
        console.log(coso)
    }
});
0 Answers
Related