put an array of object into dynamoDB

0

Hi all,
I have a problem to insert an array using the DynamoDb's standard library.
I have an API which receives this JSON:

{
    "name": "Mar/19",
    "month": "3",
    "year": "2019",
    "credit": [
        {
            "name": "Income salary",
            "value": "6500"
        }       
    ],
    "debit": [
        {
            "name": "Rent",
            "value": "2500"
        }       
    ]
}

And my backend runs on AWS and needs to put these informations on a dynamoDB table. The Lambda function look like:

const AWS = require('aws-sdk'); const dynamodb = new AWS.DynamoDB({region: 'sa-east-1', apiVersion: '2012-08-10'}); exports.handler = (event, context, callback) => {
const params = {
    Item: {
        "id": {
            S: ""+Math.random()
        },
        "name": {
            S: event.name
        },
        "month": {
            N: event.month
        },
        "year": {
            N: event.year
        },           
    },
    TableName: "billing-circle"
};
dynamodb.putItem(params, function(err, data){
    if(err) {
        console.log(err);
        callback(err);
    } else {
        console.log(data);
        callback(null, data);
    }
});

The API has a Mapping template declared on Integration Request and looks like this:

#set($inputRoot = $input.path('$'))
{
  "name" : "$inputRoot.name",
  "month" : "$inputRoot.month",
  "year" : "$inputRoot.year",
  "credit" : [
        #foreach($elem in $inputRoot.credit)
        {
            "name" : "$elem.name",
            "value" : "$elem.value"
        } 
        #if($foreach.hasNext),#end
    #end
    ],
    "debit" : [
        #foreach($elemDebit in $inputRoot.debit)
        {
            "name" : "$elemDebit.name",
            "value" : "$elemDebit.value",
            "status" : "$elemDebit.status"
        } 
        #if($foreach.hasNext),#end
    #end
    ]
}

I'm trying to call the API from Postman but I got this error:

{
    "errorType": "ValidationException",
    "errorMessage": "Supplied AttributeValue is empty, must contain exactly one of the supported datatypes",
    "trace": [
        "ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes",
        "    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)",
        "    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
        "    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
        "    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
    ]
}

Can anybody help me with this?

Afonso
asked 4 years ago4134 views
1 Answer
0

Hi Afonso,

Thanks for reaching to us on the AWS Forum. I have shared your question with our developer support team and will follow up shortly with more info.

answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions