I am using the base(’ ').create() function in a jquery script that processes my form inputs. I would like to be able to “return” the created recordID so that I can take additional actions before exiting the script.
But it appears that the recordID is not defined until the script finishes executing.
Here is the code for one of several approaches I have tried:
$(‘button#submit-deliver’).click(function() {
var data = $('form').serializeFormJSON();
var newRec = createRecord(data);
console.log('created record: ’ + newRec);
});
function createRecord(data) {
var Airtable = require(‘airtable’);
var base = new Airtable({apiKey: ‘keyLtUUC-----------’}).base(‘apptiYiTzsQcY1eNm’);
var newRec;
base('Delivery Log').create([
{
"fields": data
}
], {typecast: true}, function(err, records) {
if (err)
{
console.error(err);
return;
}
records.forEach(function (record) {
console.log(record.getId());
newRec = record.getId();
return newRec;
});
});
};
So in this case the console.log() inside the createRecord() function comes AFTER the console.log() command in the calling function.
Thanks, I bet this is pretty simple and hope to move up the learning curve just a little bit more after this!