0
votes

I have a AWS Lambda with NodeJS 10.x. This lambda is trying to scan a DynamoDB table based on a 'FilterExpression'. I am using DocumentClient to perform the 'scan' operation. I am not able to read and iterate over the resultant object despite using various methods.

I tried using the following ways: (1) var flatData = AWS.DynamoDB.Converter.unmarshall(itemdata); (2) var flatData = AWS.DynamoDB.Converter.output({"M":itemdata}); (3) var flatData = AWS.DynamoDB.Converter.output({"M":itemdata}); (1) and (3) works i.e. when I console.log the flatData I get the following: { origin: undefined, status_history: undefined, r_id: undefined, external_note: undefined, ... }

The DynamoDB table has 3 matching records for the FilterExpression that I am using. The NodeJS code also iterates 3 times but with values like mentioned above.

All these keys have values. I would like to know how can I get the values of the keys that we see above. I have tried JSON.stringify(), JSON.parse() without any luck. By the way, I am taking this approach for my Lambda: exports.handler = function(event, context, callback) {...

2
Iterate over the Items property of the returned data.jarmod

2 Answers

2
votes

You don't need to convert it into a normal javascript object since the response will be in normal javascript Object. And I prefer not to use scan operation since it will consume much more RCU. Go for a query operation.

TLDR:

The output response of scan is given below (AWS Dynamo Link )

 {
   "ConsumedCapacity": { 
      "CapacityUnits": number,
      "GlobalSecondaryIndexes": { 
         "string" : { 
            "CapacityUnits": number,
            "ReadCapacityUnits": number,
            "WriteCapacityUnits": number
         }
      },
      "LocalSecondaryIndexes": { 
         "string" : { 
            "CapacityUnits": number,
            "ReadCapacityUnits": number,
            "WriteCapacityUnits": number
         }
      },
      "ReadCapacityUnits": number,
      "Table": { 
         "CapacityUnits": number,
         "ReadCapacityUnits": number,
         "WriteCapacityUnits": number
      },
      "TableName": "string",
      "WriteCapacityUnits": number
   },
   "Count": number,
   "Items": [ 
      {
        // **Your Data Will Be Here**
      } 
   ],
   "LastEvaluatedKey": { 
      "string" : { 
         "B": blob,
         "BOOL": boolean,
         "BS": [ blob ],
         "L": [ 
            "AttributeValue"
         ],
         "M": { 
            "string" : "AttributeValue"
         },
         "N": "string",
         "NS": [ "string" ],
         "NULL": boolean,
         "S": "string",
         "SS": [ "string" ]
      }
   },
   "ScannedCount": number
}

Your expected DB fetch result will be inside the "Items" Array. And the output will not be in the Dynamodb JSON format. So no need to parse the data using their tools.

0
votes

I was making a very basic mistake because I was trying variety of options. All I had to do was the following:

data.Items.forEach(function(itemdata) {
.....
itemdata.status_history 
itemdata.r_id