Hi, I am trying to patch multiple records to airtable using Nest.js. I've checked:
- url should be correct:
https://api.airtable.com/v0/${baseId}/${tableId}
- headers with authorized token:
const headers = {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
accept: 'application/json',
};
- body is stringified with records array limited to 10:
const body = JSON.stringify({
records: records,
typecast: true,
});
my records look like:
records:[
{
id: 'recyhk6cWWQcVrG9s',
createdTime: '2023-01-09T07:07:16.000Z',
fields: {
'Code Specs': 'window.datalayer.push({\n' +
' "event": "select_content",\n' +
' "shipping_method": "${shipping_method}",\n' +
' "payment_method": "${payment_method}"\n' +
'})',
'Code Spec Match': 'false',
Platform: [Array],
'GTM trigger method': 'Datalayer',
'Event Name': [Array],
'Event Parameters': [Array],
Ecommerce: 'false',
KPI: 'select_content',
'Last Update Time': '2023-02-11T01:09:21.000Z',
'Variables Name (from Event Parameters)': [Array],
'Last modified by': [Object],
Created: '2023-01-09T07:07:16.000Z',
'Event (from Event Name)': [Array],
'Event Definition': [Array]
}
},
// in total 8 records
]
My implementation code:
/**
* patchAirtable updates one or multiple records in a table.
*
* @param promises An array of objects containing the id, field and value of the record(s) to update.
* @param baseId The id of the base.
* @param tableId The id of the table.
* @param viewId The id of the view.
* @param token The API key for the Airtable API.
*
* @returns void
*/
patchAirtable(
records: Array<any>,
baseId: string,
tableId: string,
viewId: string,
token: string,
) {
console.log('patching airtable...');
const url = `https://api.airtable.com/v0/${baseId}/${tableId}`;
const headers = {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
accept: 'application/json',
};
const body = JSON.stringify({
records: records,
typecast: true,
});
console.log(records);
this.http.patch(url, body, { headers }).subscribe(res => console.log(res));
}
I've asked GPT chatbot, but no lucks. Is there anything I missed. Any help is appreciated. One idea is that there are cells might be empty that isn't showing up in the records' fields.