Mar 11, 2019 02:52 PM
I am using Node.js library to communicate with AirTable, official js library. I am having a problem with filterByFormula. Sometimes when I try to find a record in AirTable I don’t get results even though I am pretty sure that there should be some results. My logic is such that if I do not find the record in AirTable I create a new one. If I find it I update an existing one. This logic leads to problem with same entries entered twice.
This is my query:
getReservationByID: async (externalID) { let reservations = await calendarBase('Reservations').select({ view: 'Main View', filterByFormula: `{External ID} = \"${externalID}\"`, maxRecords: 1, }).all(); return (reservations.length > 0) ? reservations[0] : null; },
And this function is used like this:
let oldReservation = await getReservationByID(reservation.id);
if (!oldReservation) {
createNewReservation(reservation);
} else {
updateReservation(reservation);
}
As you can see, it is essential for me to get record from AirTable if it exists, but sometimes AirTable is not returning an existing record with given ID and then I get duplicate with same external ID. Am I doing something wrong or is there some issue that I am not aware of?
Mar 11, 2019 03:56 PM
Try doing it without specifying a view. If no view is included, Airtable will search all records in the table which match your formula. If you specify the view, Airtable will limit its search to just that view and then find all records that match your formula. If you have a filter on the “Main View,” then you may be hiding certain records from your script.
Otherwise, your code looks fine to me. Another option is that you have a bug with reservation.id
and the value isn’t always what you expect which is then impacting your filterByFormula.
Mar 12, 2019 02:19 PM
I’ve implemented what you have suggested. So far so good. Today I didn’t have any duplicates.
Thank you.