Mar 01, 2021 10:08 PM
Hi all,
I’m at my wits end. I’ve written code for Airtable locally, which largely works. Now I’d like to place the code on a cloud functions tool so I can run my code on the browser and have it communicate with other APIs.
Here’s the code I have on the serverless tool, just the basics first just trying to call the airtable library.
/**
* main() will be run when you invoke this action
*
* @param Cloud Functions actions accept a single parameter, which must be a JSON object.
*
* @return The output of this action, which must be a JSON object.
*
*/
var Airtable = new require('airtable');
var base = new Airtable({apiKey: 'XXXXXXXXXXXXX'}).base('appumWMhUzhgN8fFe');
function main(params) {
return { message: 'Hello World' };
}
When I try to run this code, I get
Error: Cannot find module ‘airtable’\n
In this case I’m using IBM Cloud Functions, but the process as I also read Google Cloud Functions and others seems to be the same. I just am doing something wrong. Here’s the link to the docs
https://cloud.ibm.com/docs/openwhisk?topic=openwhisk-prep#prep_js_npm
Here are the steps I’ve taken.
{
"name": "my-action",
"dependencies" : {
"airtable": "^0.10.1"
}
}
//contents of my package.json
$ ibmcloud fn action create package-airtable action.zip --kind nodejs:10
You can see here the “package-airtable” gets created.
Any help would be greatly appreciated!
Solved! Go to Solution.
Mar 04, 2021 12:55 PM
Yes, your article largely worked!
One thing I needed additionally was to define a main() function in index.js. And also add export.main = main; for some reason, to deal with an error message when export wasn’t listed.
//contents of my index.js file
var Airtable = new require('airtable')
var base = new Airtable({apiKey: 'XXXXXXXXXXXXX'}).base('appumWMhUzhgN8fFe');
function main() {
return { payload: helper() }
}
function helper() {
return new Date();
}
exports.main = main;
// Returns { "payload": "2021-03-04T20:44:09.084Z"}
This ran on Cloud Functions without error. I now have to figure out how to get my Airtable functions to work and return the data I’m looking for. I’m close, but still struggling with callbacks. A new post might be coming soon.
Also, I wrote a script to package everything. The * allows me to automatically zip all contents in the folder, which saves a step of typing them out individually in your guidance.
Any time I change my index.js code located in my folder called “airtable-inc-tutorial” and want to re-upload it. I ran this script, called setup.sh, using the command: sh setup.sh
# zip my files together
zip -r function.zip *
# update the ibm cloud function with the new code
sudo ibmcloud fn action update airtable-inc-tutorial function.zip --kind nodejs:10
# invoke the code
sudo ibmcloud fn action invoke airtable-inc-tutorial --result
Even though IBM Cloud Functions has a UI, I couldn’t see a way to upload zip files, but luckily the command to write it is very simple and above. (replace “action update” with “action create” when doing this for the first time).
Mar 01, 2021 10:30 PM
Hey @J_Mai,
I literally just wrote a tutorial on this topic for AWS Lambda:
The tutorial uses a tool we built for Airtable, Sync Inc, but it covers this process for AWS Lambda and I think it will apply here.
For your issue, I would make sure you are setting up the Node.js directory properly. Go through this flow:
npm init -y
npm install airtable --save
npm modules
directory) using zip -r
(you’ll see the full details in the tutorial) and then upload it.This works well for AWS Lambda. I haven’t tried it on IBM cloud but would love to hear if these steps solve your issue.
Eric
Mar 04, 2021 12:55 PM
Yes, your article largely worked!
One thing I needed additionally was to define a main() function in index.js. And also add export.main = main; for some reason, to deal with an error message when export wasn’t listed.
//contents of my index.js file
var Airtable = new require('airtable')
var base = new Airtable({apiKey: 'XXXXXXXXXXXXX'}).base('appumWMhUzhgN8fFe');
function main() {
return { payload: helper() }
}
function helper() {
return new Date();
}
exports.main = main;
// Returns { "payload": "2021-03-04T20:44:09.084Z"}
This ran on Cloud Functions without error. I now have to figure out how to get my Airtable functions to work and return the data I’m looking for. I’m close, but still struggling with callbacks. A new post might be coming soon.
Also, I wrote a script to package everything. The * allows me to automatically zip all contents in the folder, which saves a step of typing them out individually in your guidance.
Any time I change my index.js code located in my folder called “airtable-inc-tutorial” and want to re-upload it. I ran this script, called setup.sh, using the command: sh setup.sh
# zip my files together
zip -r function.zip *
# update the ibm cloud function with the new code
sudo ibmcloud fn action update airtable-inc-tutorial function.zip --kind nodejs:10
# invoke the code
sudo ibmcloud fn action invoke airtable-inc-tutorial --result
Even though IBM Cloud Functions has a UI, I couldn’t see a way to upload zip files, but luckily the command to write it is very simple and above. (replace “action update” with “action create” when doing this for the first time).