title: Regarding Blocks method in textract using Nodejs
body: <p>In this code given below-</p>
  
  <pre><code>const _ = require("lodash");
  const aws = require("aws-sdk");
  const config = require("./config");
  
  aws.config.update({
    accessKeyId: config.awsAccesskeyID,
    secretAccessKey: config.awsSecretAccessKey,
    region: config.awsRegion
  });
  
  const textract = new aws.Textract();
  
  const getText = (result, blocksMap) =&gt; {
    let text = "";
  
    if (_.has(result, "Relationships")) {
      result.Relationships.forEach(relationship =&gt; {
        if (relationship.Type === "CHILD") {
          relationship.Ids.forEach(childId =&gt; {
            const word = blocksMap[childId];
            if (word.BlockType === "WORD") {
              text += `${word.Text} `;
            }
            if (word.BlockType === "SELECTION_ELEMENT") {
              if (word.SelectionStatus === "SELECTED") {
                text += `X `;
              }
            }
          });
        }
      });
    }
  
    return text.trim();
  };
  
  const findValueBlock = (keyBlock, valueMap) =&gt; {
    let valueBlock;
    keyBlock.Relationships.forEach(relationship =&gt; {
      if (relationship.Type === "VALUE") {
  
        relationship.Ids.every(valueId =&gt; {
          if (_.has(valueMap, valueId)) {
            valueBlock = valueMap[valueId];
            return false;
          }
        });
      }
    });
  
    return valueBlock;
  };
  
  const getKeyValueRelationship = (keyMap, valueMap, blockMap) =&gt; {
    const keyValues = {};
    console.log("Hello1");
    const keyMapValues = _.values(keyMap);
  
    keyMapValues.forEach(keyMapValue =&gt; {
      const valueBlock = findValueBlock(keyMapValue, valueMap);
      const key = getText(keyMapValue, blockMap);
      const value = getText(valueBlock, blockMap);
      keyValues[key] = value;
      console.log("Hello2");
    });
  
    return keyValues;
  };
  
  const getKeyValueMap = blocks =&gt; {
    const keyMap = {};
    const valueMap = {};
    const blockMap = {};
    console.log("Hello3");
  
    let blockId;
    blocks.forEach(block =&gt; {
      blockId = block.Id;
      blockMap[blockId] = block;
      console.log("Hello4");
      if (block.BlockType === "KEY_VALUE_SET") {
        if (_.includes(block.EntityTypes, "KEY")) {
          keyMap[blockId] = block;
          console.log("Hello5a");
        } else {
          valueMap[blockId] = block;
          console.log("Hello5b");
        }
      }
    });
  
    return { keyMap, valueMap, blockMap };
  };
  
  
  
  
  module.exports = async buffer =&gt; {
      const params = {
        DocumentLocation: {
  
  
          S3Object:{
              Bucket : "&lt;data&gt;",
              Name : "&lt;data&gt;"
          }
        },
        FeatureTypes: ["FORMS"]
      };
      const params1 = {
          JobId:"&lt;data&gt;"
  
      };
  
  
  
  
  
    const request = textract.startDocumentAnalysis(params);
    const request1 = textract.getDocumentAnalysis(params1);
    console.log("A");
  
          if(request1){
  
    console.log("B");
  
    const data1 = await request.promise();
  
          if (data1){
      console.log("C");
          if (data1 &amp;&amp; data1.Blocks) {
          console.log("D");
  
      const { keyMap, valueMap, blockMap } = getKeyValueMap(data1.Blocks);
      const keyValues = getKeyValueRelationship(keyMap, valueMap, blockMap);
  
      return keyValues;
    }
      }
    }
    return undefined;
  };
  </code></pre>
  
  <p>The output of this code is-</p>
  
  <pre><code>A
  B
  C
  undefined
  </code></pre>
  
  <p>This clearly means that data1.Blocks is returning a false value. This code is to read the pdf file using textract. Also when I remove-</p>
  
  <pre><code>if (data1 &amp;&amp; data1.Blocks)
  </code></pre>
  
  <p>I get the following-</p>
  
  <pre><code>A
  B
  C
  D
  Hello3
  (node:2975) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
      at getKeyValueMap
  </code></pre>
  
  <p>Why is the data1.Blocks empty and how to resolve the issue to give the desired output as a list of extracted words from the pdf in json format.</p>