Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Feb 16, 2021 08:33 PM
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.
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.
Solved! Go to Solution.
Feb 28, 2021 12:13 PM
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"
Feb 17, 2021 05:39 AM
Feb 17, 2021 11:28 AM
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'));
}
});
});
Feb 28, 2021 12:13 PM
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"
Feb 28, 2021 09:23 PM
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.
Feb 28, 2021 10:09 PM
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 ===.
Mar 01, 2021 11:19 AM
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.