Help

How exactly do I send an array that contains records to a table?

237 2
cancel
Showing results for 
Search instead for 
Did you mean: 
princedapace
4 - Data Explorer
4 - Data Explorer

I am using the Airtable js package for nodejs and it is my first time. I have dynamic data that I would like to store in a table. Is there a way to send the request once by passing an array into the create method? I don't want to iterate the array and call the create method on each iteration. I'm sure there is a way to do it once, I must be missing something.

const base = new Airtable({ apiKey: `${process.env.AIRTABLE_ACCESS_TOKEN}`}).base(`${process.env.AIRTABLE_BASEID}`);
 const tempOrderParticipantData = [...params?.participantData as participantDataAttributes[]];
const tempRecord: unknown[] = [];

 tempOrderParticipantData?.forEach((item, index)=>{
        tempRecord?.push({
            "Full Name": `${item?.participantNameInfo?.participantFirstName} ${item?.participantNameInfo?.participantLastName}`,
            "First Name": `${item?.participantNameInfo?.participantFirstName}`,
            "Last Name":`${item?.participantNameInfo?.participantLastName}`,
            "Phone": `${item?.participantNameInfo?.participantPhoneNumber}`,
            "Email": `${item?.participantNameInfo?.participantEmail}`,
            "Tour Name": `${params?.orderDetails?.tripName}`,
            "Payment Status": `Successful`
      })
    });
 base(`${process.env.AIRTABLE_TABLEID}`).create(tempRecord as string[]).then(record => {
  console.log('Created record');
}).catch(err => {
  console.error('Error creating record:', err);
});

I do get this error response:

Error creating record: AirtableError {
  error: 'INVALID_REQUEST_MISSING_FIELDS',
  message: 'Could not find field "fields" in the request body',
  statusCode: 422
}
2 Replies 2

Hm, `create` takes in an array of up to 10 records so you don't need to call it per record you want to create?

Each record object needs to have a `fields` date type too, so your code would need to be:

  tempRecord?.push({
         fields:{
            "Full Name": `${item?.participantNameInfo?.participantFirstName} ${item?.participantNameInfo?.participantLastName}`,
            "First Name": `${item?.participantNameInfo?.participantFirstName}`,
            "Last Name":`${item?.participantNameInfo?.participantLastName}`,
            "Phone": `${item?.participantNameInfo?.participantPhoneNumber}`,
            "Email": `${item?.participantNameInfo?.participantEmail}`,
            "Tour Name": `${params?.orderDetails?.tripName}`,
            "Payment Status": `Successful`
        }
      })



Thank you very much for coming through. Is there a place this was explained in their documentation because I have been there for hours, coupled with limited contents on this app online. 

Secondly, if I have more than 10 records in the array, how do I handle that?