API fetch and loop to table data

Hi guys, just need some help few tweaks to my code the results is looping my items. I should have 6 items to display only but the results on the image below it got loop redundant here’s my code. repeated the all list of items twice or trice

Jun22_0-1680858948632.png

async function loadDealsDetails2() {
 try {
 const dealsResponse = await fetch(
 'https://transforminator.com/_hcms/api/congratsdeal',
 requestOptions
 )
 const dealsData = await dealsResponse.json()
 console.log(dealsData)
 if (dealsData.length > 0) { 
 // Create a list of unique contact IDs
 const contactsIds = dealsData.reduce((acc, curr) => {
 const partnerContact = curr.associations.contacts.results.find(
 (result) => result.type === 'partner_contact' || curr.associations.contacts.results[0]
 )
 return [...new Set([...acc, partnerContact.id])]
 }, [])

 // Create a list of all unique company IDs
 const companiesIds = dealsData.reduce((acc, curr) => {
 const partnerCompany = curr.associations.companies.results.find(
 (result) => result.type === 'partner_company' || curr.associations.companies.results[0]
 )
 return [...new Set([...acc, partnerCompany.id])]
 }, [])

 // Create a list of deal owners
 const dealOwnersIds = dealsData.map(({ properties }) => properties.hubspot_owner_id)

 // Fetch contacts, companies, and owners details 
 const contactsDetails = await fetchObjectsDetails('contactdetails', contactsIds.join(','))
 const companyiesDetails = await fetchObjectsDetails('companydetails', companiesIds.join(',')) 
 const ownersDetails = await fetchObjectsDetails('ownerdetails', dealOwnersIds.join(','))

 const userCompanyId = document.getElementById('companyId').value 

 if (dealsData.length > 0) {
 const table = document
 .getElementById('demoC')
 .getElementsByTagName('tbody')[0]
 const companyIdd = document.getElementById('companyId').value

 dealsData.forEach((element) => {
 const row = table.insertRow()
 let cell = row.insertCell()
 cell.innerHTML =
 '<a class="hyper_linkhere_trigger_javascript" target="_blank" href="/deal-details?dealid=' +
 element['id'] +
 '">' +
 element['properties']['dealname'] +
 '</a>'
 cell = row.insertCell()
 cell.innerHTML =
 getCompanyName(companyiesDetails, element['associations']['companies']['results'][0]['id'])
 cell = row.insertCell()
 cell.innerHTML = getOwnerDetails(ownersDetails, element['properties']['hubspot_owner_id'])
 cell = row.insertCell()
 cell.innerHTML = getContactDetails(contactsDetails, element['associations']['contacts']['results'][0]['id']).fullName 
 cell = row.insertCell()
 cell.innerHTML = getLabelName(element['properties']['dealtype'])
 cell = row.insertCell()
 cell.innerHTML = '$' + element['properties']['amount']
 cell = row.insertCell()
 cell.innerHTML = getLabelName(element['properties']['dealstage'])
 })
 } 
 }
 } catch (error) {
 console.log('error', error)
 }
 }

 loadDealsDetails()
 loadDealsDetails2()

Hello @Jun22 ,

Thank you for posting in our Community! I want to invite our top experts to this conversation @stefen @Teun @Kevin-C any recommendations to @Jun22?

Thank you,

Pam

Thanks @PamCotton
Hey @Jun22
So without more context I think we’re a little limitted in the help we can provided. There may be 2 things worth looking into:

  1. You may be calling redundant functions: “loadDealsDetails() loadDealsDetails2()”
  2. ​You may have duplicates in you response data. Table log out your data before rendering it to ensure you’re getting the expected data.

Hope this gets you moving!