Home > Back-end >  NODE.js Lambda dynamodb documentClient get - returned data is not in [] JSON format. How to return a
NODE.js Lambda dynamodb documentClient get - returned data is not in [] JSON format. How to return a

Time:12-14

I'm new to Node.js/AWS lambda. Ive successfully created several documentClient QUERY functions that return a single or multiple item JSON Document in this format:

[ { "name": "andy", "color": "purple", "snack": "oreos" } ]

When I use documentClient GET and get back my single record its in THIS format, which is not playing well with the client code (apple / ios swift)

{ "name": "andy", "color": "purple", "snack": "oreos" }

I'm hopign i can change the format returned from documentClient.get() to include the fill JSON document format including leading and trailing brackets .. []

I am a node.js & aws.lambda and documentClient novice, so apologies if this is a very basic question....

provided in above text

CodePudding user response:

If I understood well, you're receiving an object instead of a array. You can use the scan function to retrieve an array of results:

var params = {
  TableName : 'Table',
  FilterExpression : 'Year = :this_year',
  ExpressionAttributeValues : {':this_year' : 2015}
};

var documentClient = new AWS.DynamoDB.DocumentClient();

documentClient.scan(params, function(err, data) {
   if (err) console.log(err);
   else console.log(data);
});

Or you can transform the result to array:

const document = await documentClient.get({
  TableName: "table-of-example",
  Key: {
    id: "id-of-example"
  }
});

return [data]

Please read the document to understand more how the document dynamodb sdk works: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property

  • Related