GV14982 / async-airtable

A lightweight npm package to handle working with the Airtable API.
https://www.npmjs.com/package/asyncairtable
MIT License
53 stars 5 forks source link

"+" character not supported in where query #70

Closed AntCas closed 2 years ago

AntCas commented 3 years ago

Describe the bug Query not working on emails that include "+" character.

To Reproduce Steps to reproduce the behavior:

  1. Create an airtable text column
  2. Add a string like "test+001@gmail.com"
  3. Attempt to query for the string:
  const records = await Airtable.select('Table_Name, {
    where: {
      primary_email: "test+001@gmail.com",
    }
  });
  1. See that no records are returned.
  2. Remove the "+" from the email in the query and in Airtable, try again, records are returned.

Expected behavior 1 record returned for the row w/ column containing the string.

Additional context

Also tried using encodeURIComponent and filterByFormula, neither worked.

  const records = await Airtable.select('Table_Name, {
    where: {
      primary_email: encodeURIComponent("test+001@gmail.com"),
    }
  });
GV14982 commented 3 years ago

Hm, I'll take a look at this. It's possible it's not getting encoded correctly, or being tossed out.

AntCas commented 3 years ago

I think it's an encoding problem, but I didn't go down the rabbit hole deep enough to find where.

Since I only currently need to make a single Airtable call I ended up going back to the vanilla airtable api and wrapping it in my own Promise to use it w/ async/await.

btw I found this project by searching "Airtable api with async await" or something similar :)

AntCas commented 3 years ago

Here's my async code for the next lost engineer just trying to get async working quickly:

async function getAirtableRecordsForUser(email: string) {
  const base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_BASE_ID);

  const promise = new Promise((resolve, reject) => {
    let user_records = [];

    base("TABLE_NAME")
      .select({
        filterByFormula: `primary_email="${email}"`
      })
      .eachPage(
        function page(records, fetchNextPage) {
          records.forEach(record => {
            user_records.push({
              email: record.fields.primary_email,
              address: record.fields.Address,
              status: record.fields.Status
            });
          });
          fetchNextPage();
        },
        function done(err) {
          if (err) {
            reject(err);
          } else {
            resolve(user_records);
          }
        }
      );
  });

  try {
    const response = await promise;
    return response;
  } catch (err) {
    console.log(err);
    return [];
  }
}

Watch out for lack of error handling to you future engineer who copy-pastes this.

GV14982 commented 3 years ago

Hey there, so I pushed this into the next branch. Can you install asyncairtable@next and let me know if you're still having this issue?

GV14982 commented 2 years ago

Hey there @AntCas have you had a chance to test this on the @next branch? Just want to be sure it's working before I merge into main.

AntCas commented 2 years ago

Hey @GV14982 I won't be able to test this, apologies. Glad a fix was found!