Help

HELP! What should have been simple is my nightmare today

Topic Labels: Automations
196 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Francois_Bedard
5 - Automation Enthusiast
5 - Automation Enthusiast

Simply put, I cannot understand why my "listeCommandites" Array shows 20 elements before I loop through it and then the console.log(item) within the loop only iterates through 17 elements. Lines i'm talking about for console.log() are in the last code snippet. Here's a code sample:

...

 

 

async function createOrUpdateCommandite(pPostId, pRecordID, nom, status, meta) {
    const headers = {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    };

    const postData = {
       "title": nom,
       status,
       "meta": meta
    };

    let response;
    try {
        if (pPostId) {
            // Update existing post
            response = await fetch(`${wpDomain}/wp-json/wp/v2/commanditaires/${pPostId}`, {
                method: 'POST',
                headers,
                body: JSON.stringify(postData)
            });
        } else {
            // Create new post
            response = await fetch(`${wpDomain}/wp-json/wp/v2/commanditaires`, {
                method: 'POST',
                headers,
                body: JSON.stringify(postData)
            });
        }

         const responseData = await response.json();

        if (response.ok) {
            console.log(`Commandite ${responseData.id} ${pPostId ? 'updated' : 'created'} successfully.`);
            await commanditeTable.updateRecordAsync(pRecordID,{
            "WP Commandite Post ID": responseData.id
            })   
        } else {
            console.error(`Error ${pPostId ? 'updating' : 'creating'} commandite:`, responseData.message);
        }
    } catch (error) {
        console.error(`Error ${pPostId ? 'updating' : 'creating'} commandite:`, error.message);
    }
}

 

 

...

 

 

async function getListeCommandites(pCommandites) {
    if (pCommandites !== null) {
        for (let commandite of pCommandites) {
            const commanditeRecord = queryCommanditeTable.getRecord(commandite.id);
            if(commanditeRecord !== null && commanditeRecord.getCellValue("Statut").name == "Confirmé") {
                listeCommandites.push({
                    'id': commandite.id,
                    'commandite-post-ID': commanditeRecord.getCellValue("WP Commandite Post ID") === null ? null : commanditeRecord.getCellValue("WP Commandite Post ID"),
                    'commandite-image-id': commanditeRecord.getCellValue("Web - commandite image ID"),
                    'commandite-lien': commanditeRecord.getCellValueAsString("Web - commandite lien"),
                    'commandite-nom': commanditeRecord.getCellValue("Nom complet (from Client)"),
                    'commandite-debut': toTimestamp(commanditeRecord.getCellValue("Commandite - Début affichage web (from Salon)")),
                    'commandite-fin': toTimestamp(commanditeRecord.getCellValue("Commandite - Fin affichage web (from Salon)")),
                    'commandite-recordID': commanditeRecord.id
                })
            }
        }
    }
}

 

 

...

 

 

getListeCommandites(commandites).then(()=> {
    console.log(listeCommandites); // HERE, THE LOGS SHOWS 20 ARRAY ENTRIES
    //for (let item of listeCommandites) {
    listeCommandites.forEach(function (item) {
        console.log(item); // HERE, THERE IS ONLY 17 ITERATIONS
        let commPostID = item["commandite-post-ID"];
        let meta = {
            'commanditaire-image-id': item["commandite-image-id"] === null ? null : item["commandite-image-id"].toString() ,
            'commanditaire-site-web': item["commandite-lien"],
            'debut-affichage': item["commandite-debut"],
            'fin-affichage': item["commandite-fin"]
        }
        createOrUpdateCommandite(commPostID, item["commandite-recordID"], item["commandite-nom"][0], 'publish', meta)
    })
})

 

 

 

0 Replies 0