Unable to update my table via code
# 🤝help
e
Hey there I have a webhook to delete everything on a table and then write the payload values, the delete is working but not the writing, can someone help me debug this? This is my code: const sheetsInformation = event.payload.body.events async function deleteAllRecords() { try { const data = await syncKBTable.findRecords({ selectorName: 'allRecords' }) if (!Array.isArray(data)) { console.log('No records found.') return } const recordIDs = data.map((record) => record.id) if (recordIDs.length > 0) { await syncKBTable.deleteRecords(recordIDs) console.log('Old records deleted.') } else { console.log('No records to delete.') } } catch (error) { console.log(error) } } async function createNewRecords(sheetsData) { try { const records = sheetsData.map((sheet) => ({ Name: sheet[0], Location: sheet[1], Date: sheet[2] })) await syncKBTable.createRecords(records) } catch (error) { console.log(error) } } await deleteAllRecords() await createNewRecords(sheetsInformation) This is my payload.body: {"events":[{"0":"Pool Party","1":"Trindade","2":"10 de abril"},{"0":"Beach Party ","1":"ET","2":"04/11"}]} Basically it is an array of objects each object with 3 key value pairs that are strings. The fields in the table are Name, Location and Date
r
In your logs what error do you see?
f
He is getting this error.
Copy code
Execute operation createRecords() failed: request/body must have required property 'rows'
  [Error, request/body must have required property 'rows']
I will try and fix it today
r
I see. That means
records
is empty.. maybe the payload.body is a string instead of JSON?
f
You are completely right that records was empty. It was receiving an array of array, instead of an array of objects. It returned
undefined
or
NULL
because the properties
sheet[0]
,
sheet[1]
, and
sheet[2]
don't exist in the inner arrays. I fixed it by adjusting the mapping logic inside the
createNewRecords
function. I also adjusted some other things the Make.com scenario, mostly the create JSON module. This is the updated code that creates the records.
Copy code
javascript
async function createNewRecords(sheetsInformation) {
  try {
    const records = sheetsInformation.map((sheetInfo) => ({
      Name: sheetInfo[0],
      Location: sheetInfo[1],
      Date: sheetInfo[2]
    }))
    console.log(records)
    await syncKBTable.createRecords(records)
  } catch (error) {
    console.log(error)
  }
}
Here I have set the parameter name to
sheetInfo
to represent each individual array within the
sheetsInformation
array. So the mapping logic is adjusted to access the elements of each inner array by using
sheetInfo[0]
,
sheetInfo[1]
, and
sheetInfo[2]
respectively. This change makes it so that our
records
array now contains the correct objects with the
Name
,
Location
, and
Date
properties populated from the
sheetsInformation
array.
2 Views