I’m using the Airtable API to create an entry in one of my bases. I can get the entry to create no problem, the issue is I would like to return the entry id after the creation and it returns ‘undefined’. See me code below, I can get the id number to print out at the point shown, but I can’t send it out to my other functions.
I’m pretty new to this, I’m sure it’s just a simple oversight, thank you in advance.
export function getCustomer (){
var nCustomerID;
base(‘Customers’).create({
“Name”: “John Smith”,
“Orders”: [
]
}, function (err, record) {
if (err) {
console.error(err);
return ;
}
nCustomerID = record.getId();
//console.log here I get the record id
});
//console.log here, returns ‘undefined’
return nCustomerID;
}
Also, simply try moving the second console log to underneath the return, otherwise you might be getting the console log for the getID and then you are getting nothing else before you do the second console log.
Thanks for all the info! I’ve use Wix package manager to install the Airtable node.js. I’m able to access my Airtable tables perfectly and create and retrieve info.
I’ve found that if I pass record.getId() to another function at the first console log I can utilize the ID in that new function. The strange thing is I can’t set the nCustomerID variable to record.getId(), it just doesn’t change it.
Basically if I have nCustomerID = “Hello” in an upper scope, at the point of setting it to record.getId(), it just doesn’t update. In fact I won’t even update to a different string at that point either (ie nCustomerID = “goodbye”).
Seems maybe scoping issue, but I have no idea!
I‘ve also posted this on the Airtable community forum, nothing there yet.
Fingers crossed that you get a suitable answer soon to your question on this forum or on the AirTable forum too - https://community.airtable.com/t/creating-entry-from-wix-returning-undefined/23580 (found you!)
A good person to ask would have been Andreas Kviby (WixShow) as he has dealt with AirTable within Wix already and set it all up etc. Alas though, he has headed off for new pastures that paid and therefore a lot of his old WixShow website does not work anymore.
You can see his old posts about it here and you do have the option to purchase his code for it too, although note that the shop link is a good many months old so don’t go buying immediately without checking that it is still available etc.
https://www.wix.com/corvid/forum/community-discussion/use-airtable-as-data-in-your-repeaters
https://andreaskviby.wixsite.com/complete-course-site/airtable-as-datasource
https://www.wixshow.com/
Obviously, you can simply embed a AirTable form in Wix through using a html iframe, however it would be great if we could have more access to it, unless you are happy to go and use Google Sheets instead as you can already implement Google Sheets and Forms etc into Wix already.
https://support.wix.com/en/article/uploading-a-google-spreadsheet-to-the-table-master-app
https://www.wix.com/corvid/forum/community-discussion/example-google-sheets-npm
Although the fact that it is already in Wix Node Manager since June 2018, then it should be a easy and hopefully quick fix when somebody with more help and knowledge about it comes to your aid.
Also, if you sign into AirTable own API section, then you should be able to get node.js examples, as shown here.
https://flaviocopes.com/airtable/