I'm using a Lambda function to index records into Algolia. I'm able to get the data via the event object. I'am running into a problem where the saveObject function of Algolia doesn't seem to call or it returns before finishing what it is doing. Bit hard to debug as well since I have to look at cloudwatch every time. I threw some console.logs here and there to see if the flow is correct. I'm kind of stuck here. Any help is much appreciated.
search.js
'use strict';
const algoliasearch = require('algoliasearch');
const ALGOLIA_APP = 'appid';
const ALGOLIA_KEY = 'algoliakey';
const INDEX = 'indexname';
const connectToIndex = (appId,apiKey,index) => {
const client = algoliasearch(appId,apiKey);
return client.initIndex(index);
};
const updateIndex = async (record) => {
console.log('updating/replacing') //logs in cloudwatch
const index = connectToIndex(ALGOLIA_APP,ALGOLIA_KEY,INDEX);
console.log(record,index); //logs the record object and index object in cloudwatch
index.saveObject(record)
.then(()=>{
console.log('updated/replaced') //doesn't log in cloudwatch + not indexed in algolia
return;
}
).catch((e) => {
console.error(e);
return;
});
}
module.exports.algoliaIndexer = async (event, cb) => {
try {
const body = JSON.parse(event.Records[0].body);
const record = {...restructuring body}; //record to index
if (body.detail.operationType === 'update' || body.detail.operationType === 'replace'){
updateIndex(record);
console.log('done1') //logs in cloudwatch
}
console.log('done2') //logs in cloudwatch
cb(null, responseObj('success', 200));
} catch (e) {
console.error(e);
cb(null, responseObj(e.message, 500));
}
};
handler.js contains the lambda function
'use strict';
const search = require('./lib/search');
let algoliaIndexer = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
search.algoliaIndexer(event, callback);
};
module.exports = {
algoliaIndexer : algoliaIndexer
}
EDIT
I tried with await updateIndex(record) as well with no luck