title: Getting undefined after parsing the data in node js
body: <p>I am trying to upload an image to S3 from a AWS lambda function. For this I have followed this tutorial - <a href="https://medium.com/think-serverless/image-upload-and-retrieval-from-s3-using-aws-api-gateway-and-lambda-b4c2961e8d1" rel="nofollow noreferrer">link</a>. I have created an post API using API Gateway and written nodejs code in lambda to post an image to S3. But I am getting below error from API Gateway while trying to test the API.</p>
  
  <pre><code>"errorType": "TypeError [ERR_INVALID_ARG_TYPE]",
  "errorMessage": "The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined",
  "code": "ERR_INVALID_ARG_TYPE",
  "stack": [
      "TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined",
      "    at Function.from (buffer.js:207:11)",
      "    at Runtime.exports.handler (/var/task/index.js:17:32)",
      "    at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)",
      "    at process._tickCallback (internal/process/next_tick.js:68:7)"
  </code></pre>
  
  <p>My lambda code looks like below:-</p>
  
  <pre><code>const AWS = require('aws-sdk');
  //*/ get reference to S3 client 
  var s3 = new AWS.S3();
  
  
  exports.handler = (event, context, callback) =&gt; {
  
  
       let encodedImage =JSON.parse(event.body).user_avatar;
       let decodedImage = Buffer.from(encodedImage, 'base64');
       var filePath = "avatars/" + event.queryStringParameters.username + ".jpg"
       var params = {
         "Body": decodedImage,
         "Bucket": "find-my-mate-hasangi",
         "Key": filePath  
      };
      s3.upload(params, function(err, data){
         if(err) {
             callback(err, null);
         } else {
             let response = {
          "statusCode": 200,
          "headers": {
              "my_header": "my_value"
          },
          "body": JSON.stringify(data),
          "isBase64Encoded": false
      };
             callback(null, response);
      }
      });
  
  };
  </code></pre>
  
  <p>I am getting error in below line:-</p>
  
  <pre><code> let decodedImage = Buffer.from(encodedImage, 'base64');
  </code></pre>
  
  <p>I am passing below body from API Gateway:-</p>
  
  <pre><code>{
    "body": {
      "user_avatar": "asas"
    }
  }
  </code></pre>
  
  <ul>
  <li>Update: I have tried some debugging after suggestions in comments</li>
  </ul>
  
  <blockquote>
  <pre><code>console.log( event.body);         // Output: { "body": { "user_avatar": "Hi There!" } }
  console.log( JSON.parse(event.body)); // Output: { body: { user_avatar: 'Hi There' } } 
  console.log( event.body.user_avatar);               // Output: undefined
  console.log( JSON.parse(event.body).user_avatar);   // Output: undefined
  </code></pre>
  </blockquote>
  
  <p>The code is written in Node JS v6.10 which is not available in lambda anymore. So I am using Node JS v10.x. Can it be the reason for the error? Anyone know how to resolve the error.</p>