Cannot access variable before initialization

Hi,

Greetings!

today I encountered this below error

    const body = {
        freshdesk_webhook:{ 
            ticket_id: ticketID,
            ticket_tags: payloadTag,
            ticket_status: payloadStatus,
            ticket_priority: payloadPriority,
            ticket_source: ticketData.source,
            ticket_url: `https://${freshdeskSubdomain}.freshdesk.com/api/v2/tickets/${ticketID}`,
            agent_email: agentEmail,
            ticket_contact_sla_level: ticketData.custom_fields.sla_level
        }
    }


    console.log(body)
    console.log(new Date())
    // return {status : 1 , data : body}
    return new Promise((resolve) => {
        try {
           //logic
        } catch (e) {
            console.error(`Error while triggering webhook for #${ticketID}`);
            console.log(e)
            resolve({ status: 0, data: e });
        }
    });

Can someone please assist me here on why this is happening?

Thank you

Hi @Bene_Immanuel,

The error “ReferenceError: Cannot access ‘body’ before initialization” indicates that the variable body is being referenced within a scope before its actual declaration. This typically occurs when a variable is in the Temporal Dead Zone.

Based on the error log pointing to server.js:51:41, it is suggested that:

  • A second declaration of const body or let body may exist further down in the triggerWebhook function or within the try { //logic } block.

  • When a variable is redeclared in the same scope, the entire block is bound to that declaration, rendering any previous reference to body invalid until that specific line is executed.

To resolve this, the file server.js should be checked around line 51. Any duplicate declarations of the body variable should be renamed or consolidated to ensure the variable is fully initialized before it is accessed by the console.log or the Promise logic.

Regards,

Himanshu

Hi @Himanshu_Sharma,

Thank you for your reply, but there is no other variable in the function with the name “body“.

and it looks like this is happenning recently.

Thank you.

Hi @Bene_Immanuel, please let me know if the issue still persists.