AirTable API Find record by email

≡放荡痞女 提交于 2020-06-01 03:24:58

问题


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.

回答1:


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!



来源:https://stackoverflow.com/questions/60825822/airtable-api-find-record-by-email

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!