Help

Re: How can I do a basic lookup on Airtable using Node js?

Solved
Jump to Solution
5985 0
cancel
Showing results for 
Search instead for 
Did you mean: 
J_Mai
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi everyone - I’m a total beginner to Node js but it’s very frustrating, that after hours of searching on Airtable Docs as well as through the community, I can’t seem to find the answer to one of the most basic things a user would want to do with a database.

Screen Shot 2021-02-16 at 8.23.23 PM

In short - I’m trying to do a lookup. Given a “phone number” of “(454) 382-9029”, I want to return the “Full Name” of “Carol Lerner”. I want Airtable to access the right record and the code to print the “Full Name”.

In Excel this takes 2 seconds to do. I’m at a loss why this has been so difficult to find code for on Airtable Docs or Community on Node js. I know that I’m a beginner. I’m not sure why it’s so difficult to see how to do something like this. My code won’t really be of much help here as I keep getting errors when I try to run it.

Thanks for taking a look and hope you can help.

1 Solution

Accepted Solutions
J_Mai
5 - Automation Enthusiast
5 - Automation Enthusiast

I was able to eventually figure this out. Here’s the code for anyone interested.

What was wrong was that I needed to use a “===” operator. I wish Airtable docs had something to show “If statements” so I could have learned this much faster.

var carolNumber = "(454) 382-9029"

base('Table1').select(
    {
      maxRecords: 3,
      view: 'Grid view'
    }
  ).firstPage(function page(err, records)
    {
      if (err) { console.error(err); return;  }
      records.forEach(function(record) {
          if(record.get('Phone Number') === carolNumber)
          {
          console.log('Lookup is', record.get('Full Name'));
          }
        });
      });

// Returns "Lookup is Carol Lerner"

See Solution in Thread

6 Replies 6

Hi @J_Mai, and welcome to the community!

And yet, with an insight into what you’re trying, community helpers will be able to avoid wasting their own time starting from scratch. I recommend you share ALL your details if you want to get people to help you. If not, take a look at this.

J_Mai
5 - Automation Enthusiast
5 - Automation Enthusiast

Okay, great point @Bill.French. Here’s the code.

var carolNumber = "(454) 382-9029"

base('Table1').select(
    {
      maxRecords: 3,
      view: 'Grid view'
    }
  ).firstPage(function page(err, records)
    {
      if (err) { console.error(err); return;  }
      records.forEach(function(record) {
          if(record.get('Phone Number') = carolNumber)
          {
          console.log('Lookup is', record.get('Full Name'));
          }
        });
      });
J_Mai
5 - Automation Enthusiast
5 - Automation Enthusiast

I was able to eventually figure this out. Here’s the code for anyone interested.

What was wrong was that I needed to use a “===” operator. I wish Airtable docs had something to show “If statements” so I could have learned this much faster.

var carolNumber = "(454) 382-9029"

base('Table1').select(
    {
      maxRecords: 3,
      view: 'Grid view'
    }
  ).firstPage(function page(err, records)
    {
      if (err) { console.error(err); return;  }
      records.forEach(function(record) {
          if(record.get('Phone Number') === carolNumber)
          {
          console.log('Lookup is', record.get('Full Name'));
          }
        });
      });

// Returns "Lookup is Carol Lerner"

Glad you got it working.

That’s like saying…

I wish Airtable documented every aspect of javascript.

That would be vast and it’s really not Airtable’s job to document the entirety of javascript. Working with script resources in every platform comes with a certain degree of knowledge and while this doesn’t lessen the anguish of learning (for the first time) that an “=” operator is how one assigns values, it’s one of those very essential concepts in software engineering that are best learned in class or in tutorials.

J_Mai
5 - Automation Enthusiast
5 - Automation Enthusiast

Thank you! Yeah, I agree. But beefing up the Airtable docs a bit would help.

Spent at least 6 hours between today and yesterday just trying to figure out how to call Airtable on a serverlerss cloud function. I still don’t totally know if I’m doing it right, but at least I was able to make a call and return a table. Didn’t realize I needed this require (‘request-promise’) thing to actually speak to the airtable API on a serverless set up.

I kept searching on Google for things like:

“how to call airtable functions when outside of local machine api”
“how to add airtable library to browser”
“import airtable library api”
“how to call airtable api in serverless”
“how to run new require(‘airtable’) on server”
“how do i communicate with airtable api using cloud functions”
“how to call airtable api using node js”
“how to initialize airtable in aws lambda”
“call an api using node js”

None of those really got to the answer. But I learned a bunch and it only took two days :laughing:

Figured out that I needed to use a library that’s built into node js called “request-promise” that I could call from my cloud functions service. This also explained why I could call the Airtable API fine on my computer, but could not call it on the cloud functions service I’m using.

Here’s the code for posterity in case others run into any of the same searches above to try to call the Airtable API from a serverless tool like Google Cloud Functions, AWS Lambda, IBM Cloud Functions, or Azure Functions.

let rp = require('request-promise')

function main(params) {
    const options = {
        url: "https://api.airtable.com/v0/appumWMhUzhgN8fFe/Table1?api_key=XXXXXXXXXX",
        json: true
    }
    return rp(options)
    .then(res => {
        return { response: res }
    })
}
///returns a JSON file with my table of data

I know there are other ways to do this, but I haven’t figured those out yet.

It has been about 15 years since my last formal class in computer science, I understand @Bill.French you’re a self-taught person as well, having read some other replies in this community. So thanks for the encouragement. I admit I did make the newbie mistake of = being different than == or ===.

Ha ha - welcome to the world of software integration.

No one does.

You’re an expert now (of sorts). :winking_face:

This all depends on the scripting engine. NodeJS is a non-blocking architecture as compared to Google Apps Script v8 engine which is capable of blocking and non-blocking execution. I prefer Google Apps Script because I can be lazy and simple and fast.

That’s 190 days fewer than most. Ask any golfer what the first two days were like.