Confused with .find() and node.js


#1

Hi, I’m new to the world of javascript and node.js – actually, I haven’t written much code for a decade, but I’m interested in building an Alexa skill which pulls data from an Airtable base.

I’m stuck with this function (below or on pastebin). Everything that gets displayed on the console is what I expect, however, I can’t work out how to get the function to return the string ‘Series Title’ (I know it’s something to do with the mystery of asynchronous code).

Any ideas of where I’ve gone wrong (or pointers to tutorials/examples to help get my head around this).

GPDataHelper.prototype.getSermonSeries = function(date){
var err, records, weekRecord, seriesRecord;

var series, seriesID, seriesTitle;
var filter = 'IS_AFTER({Date}, "' + date + '")';

base('Week').select({
maxRecords: 1,
view: 'Main View',
filterByFormula: filter,
sort: [{field: "Date"}]
}).firstPage(function(err,records) {
	if (err) { console.error(err); return; }
	records.forEach(function(weekRecord){
		console.log('Retrieved', weekRecord.get('Date'));
		series = weekRecord.get('Series');
		
		series.forEach(function(seriesID) {
			base('Sermon Series').find(seriesID, function(err, seriesRecord) {
	    	if (err) { console.error(err); return; }
    		console.log(seriesRecord.get('Series Title'));
			seriesTitle = seriesRecord.get('Series Title');
			return seriesTitle;
			})
		})
	})
})
}

#2

Ok - I’ve got things working much better now. For others learning the ins-and-outs of async coding, the project is at https://glitch.com/edit/#!/gp-skill and this code snippet comes from gp_data_helper.js.