2

I'm trying to see if a user exists by trying to find their email in the user's table, if it doesn't exist then I'm inserting the user.

var Airtable = require('airtable');
var base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(process.env.AIRTABLE_BASE_ID);

app.post('/addUser', (req, res) => {
  const { uid, displayName, email, photoURL, provider } = req.query;
  base('users').find({filterByFormula: `FIND(email = '${email}')`}, function(err, record) {
    if (err) res.status(400).send(err);
    console.log('Retrieved', record.id);
    base('users').replace([{"fields": { uid, displayName, email, photoURL, provider}}], function(err, records) {
      if (err) console.log(err)//res.status(400).send(err);
      else console.log(records[0]) 
      res.status(200).send(records[0]._rawJson);
    });
  });
});

The error I get is

] AirtableError {
[api]   error: 'NOT_FOUND',
[api]   message: 'Could not find what you are looking for',
[api]   statusCode: 404
[api] }

API docs are here: https://support.airtable.com/hc/en-us/articles/203255215-Formula-Field-Reference

I also tried

  base('users').select({filterByFormula: `FIND(email = '${email}')`}, function(err, record) {....

and got the error

Airtable: `select` takes only one parameter, but it was given 2 parameters. Use `eachPage` or `firstPage` to fetch records.
Bill
  • 4,614
  • 13
  • 77
  • 132

1 Answers1

9

find method can only retrieve record with a record-id parameter. for example like,

base(table_name)
   .find(record_id, function (err, record) 
       {...}
   )

To retrieve filtered record, there is select method with filterByFormula parameter. for example like,

base(table_name)
   .select({
      filterByFormula: `email = "${email}"`
   }).firstPage((err, records) 
      {...}
   )

Or if we need to retrieve record not like email,

filterByFormula: `NOT(email = "${email}")`

Above filter retrieve all records or record that's email is not equal to desired email. We can also use other such as AND and etc.

Note: You can use the following parameters to filter, sort, and format the results:

  • fields e.g. fields: ["Reference No.", "Name of the Candidate"]
  • filterByFormula e.g. filterByFormula: "NOT({Reference No.} = '')"
  • maxRecords e.g. maxRecords: 1
  • pageSize e.g. pageSize: 100
  • sort e.g. sort: [{field: "Reference No.", direction: "desc"}]
  • view e.g. view: "My View"

To find more info about above listed parameters please check API Documentation of your base.

Hope this helps!

Aditya
  • 3,525
  • 1
  • 31
  • 38
  • How did you learn to use Airtable as an API? I'm trying to figure out things like advanced queries, querying linked records (eg a user's projects), and pagination. And all of the tutorials I see on the site are for using the GUI and not for the API. How did you do it? – Maiya May 22 '20 at 03:58
  • `Curl` and `JavaScript` documentation available in the Api documentation on Airtable. Explanation about filters, fields and other methods are available there, you just need to add some dummy data. – Aditya May 22 '20 at 07:04