Home > front end >  Algolia saveObject not called inside Lambda function using Serverless framework
Algolia saveObject not called inside Lambda function using Serverless framework

Time:01-12

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

CodePudding user response:

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.

That's the issue. You are not waiting for the saveObject function to finish (return the result). To do this you need to await for your async functions return (the concept is called async/await).

So, try changing this line:

updateIndex(record);

to

await updateIndex(record);

This should fix the issue.

Furthermore, you are using an async handler with a callback. That might also break something. You either use an async handler OR a callback, not both.

Try the following handler signature:

module.exports.algoliaIndexer = async (event) {
  ... your code ...
}

Relevant documentation: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

CodePudding user response:

using a try catch block in the updateIndex function combined with @Jens's answer solved the issue for me.

search.js

const updateIndex = async (record) => {
  const index = connectToIndex(ALGOLIA_APP,ALGOLIA_KEY,INDEX);
  try{
    await index.saveObject(record);
  }
  catch(e){
    console.error(e);
  }
}

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'){
        await updateIndex(record);
      }
      cb(null, responseObj('success', 200));
    } catch (e) {
      console.error(e);
      cb(null, responseObj(e.message, 500));
    }
  };
  •  Tags:  
  • Related