Duplicate records in my customerTable
# 🤝help
We're making an api call after the webchat is triggered that populates a table customerTable with existing customer's data as a way to create a personalized chat experience by matching a passed uid in the referring URL to the same uid in the table. The table should only have new records added when there are new records in the data passed from the api but right now everytime the webchat is triggered all of the customer records are duplicated. I can't figure out how to add only new customer records when triggered. Here is the code being used for the api call. const url = 'https://api.hostfully.com/v2/guests'; const params = { agencyUid: '' }; const headers = { 'X-HOSTFULLY-APIKEY': '' }; try { const response = await axios.get(url, { params, headers }); if (response.status === 200) { const guests = response.data.filter(guest => guest.firstName && guest.lastName); // Fetch existing UIDs from the table const existingUids = (await customerTable.findRecords()).map(record => record.uid); // Populate the
with guest data for (const guest of guests) { if (!existingUids.includes(guest.uid)) { // Create a new record in the
with the guest data await customerTable.createRecord({ firstName: guest.firstName, lastName: guest.lastName, phoneNumber: guest.phoneNumber, uid: guest.uid, email: guest.email, }); existingUids.push(guest.uid); // Add UID to the list of existing UIDs } } console.log('Data successfully populated in the customerTable'); } else { console.log(
Response status: ${response.status}
); } } catch (error) { console.log('An error occurred:', error.message); }