Returning results from an array using Code by Zapier

The input of the party_identifiers are a comma separated IDs, for example 123,124,125
for each of the ID,

the JS would try to fetch the personal information (names/phone) for 123 – 125, which would contain the first/last name, title, especially it’ll try to fetch the value when contact method = phone.

The output is expected to be that 3 lines of personal information

However, the code would not run and displayed an error as below.

Would anyone be able to shed some light on it?

thanks and appreciate it.

const partyIdentifiers = inputData.party_identifiers.split(','); // Use inputData.party_identifiers for multiple party identifiers
const username = inputData.USRNAME; // Use inputData.USRNAME for the username
const password = inputData.PASSWRD; // Use inputData.PASSWRD for the password
const credentials = btoa(`${username}:${password}`);

const headers = {
  'Authorization': `Basic ${credentials}`,
  'Content-Type': 'application/json',
  'Accept': 'application/json'
};

async function getPartyDetails(partyIdentifier) {
  const url = `https://website.com/${partyIdentifier.trim()}`;

  const res = await fetch(url, {
    method: 'GET',
    headers: headers
  });

  if (!res.ok) {
    throw new Error(`HTTP error! status: ${res.status}`);
  }

  return res.json();
}

(async () => {
  let output = { details: '' }; // Ensure output is always defined
  let details = [];

  for (const partyIdentifier of partyIdentifiers) {
    try {
      const data = await getPartyDetails(partyIdentifier);
      const phoneContact = data['contact-details'].find(contact => contact['contact-type'] === 'Phone');
      
      if (phoneContact) {
        details.push(`${data['party-identifier']} / ${data['title']} / ${data['first-name']} / ${data['surname']} / ${phoneContact['value']}<br><br>`);
      } else {
        details.push(`${data['party-identifier']} / ${data['title']} / ${data['first-name']} / ${data['surname']} / No phone contact found<br><br>`);
      }
    } catch (error) {
      details.push(`Error fetching details for ${partyIdentifier}: ${error.message}<br><br>`);
    }
  }

  output.details = details.join('');
  callback(null, output);
})();

Error from the Zapier Test Troubleshoot

Failed to run your JavaScript code
string: You did not define `output`! Try `output = {id: 1, hello: await Promise.resolve("world")};`
Click on the Troubleshoot tab below or learn more in our help center.

I have tried to include the Output explicitly but still the JS won’t work