Search code examples
aws-lambdaaws-sdk-jsamazon-textract

AWS Textract does not run callback in error free run of analyzeDocument


CURRENTLY

I am trying to get AWS Textract working on a Lambda function and am following documentation on https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Textract.html#analyzeDocument-property

My Lambda code:

"use strict";

const AWS = require("aws-sdk");

exports.handler = async (event) => {
  let params = JSON.parse(event.body);
  console.log("Parse as document...");
  let textract = new AWS.Textract();
  let doc = params["doc"];
  let config = {
    Document: {
      Bytes: doc,
    }
  };
  textract.analyzeDocument(config, function (err, data) {
    console.log("analyzing...");  //<-- nothing logged to console if no error
    if (err) {
      console.log(err, err.stack);
    }
    // an error occurred
    else {
      console.log("data:" + JSON.stringfy(data));  //<-- nothing logged to console if no error
    } // successful response
  });
  console.log("Finished parsing as document.");
};

ISSUE

I cannot get the data back from Textract. It seems I am unable to get the callback working entirely. What's odd is if there is an error e.g. my configuration is wrong, the error handling of the callback will print the log and "analyzing..." log, but without error, none of the logs in the callback print.

Current Logs:

Parse as document...
Finished parsing as document.

Expected / Desired Logs:

Parse as document...
analyzing...
data:{textract output}
Finished parsing as document.

Please help!

NOTES

  • I am using a role for that Lambda that allows it to access Textract.
  • I get the same result whether I include the HumanLoopConfig settings or not.

Solution

  • Solved, apparently I needed to setup a promise:

     let data = await textract.analyzeDocument(config).promise()
     console.log("data:"+data );
     console.log("Finished parsing as document.")