3

I am trying to enter the data in AWS Dynamo DB through the AWS Lambda function using AWS HTTP API. FYI The data type of the parameter (Id) originally in Dynamo DB is Number but it is taking as String while parsing JSON data, so I have written "Number" beside "Id" parameter in order to convert it to "Number". When I am trying to run this lambda function I am getting this error. Please help, Thanks!

Lambda function:

payload: { "Id": $input.json('$.Id') "name": $input.json('$.name')

console.log('starting function');
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function(event, ctx, callback) {

  var params = {
    Item: {
      Id: Number(event.Id),
      name: event.name
    },

    TableName: 'Test'
  };
  console.log(params)
  docClient.put(params, function(err, data) {
    if(err) {
      callback(err, null);
    } else{
      callback(null, data);
    }
  });
  
}

Error log:

enter image description here

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117

1 Answers1

2

Look at the logs.

Your event.Id value is "NaN" which means "not a number".

Also event.name is "undefined".

So your problem is occuring here:

exports.handler = function(event, ctx, callback) {

Your event object is not populated with the values you are expecting.

The payload should be proper JSON and look something like:

 {
    "id": "6",
    "name": "Test Name"
 }

To achieve this, in your POST from your front-end code you could use something like:

data: JSON.stringify({"id": $('#Id').val(), "name": $('#name').val()})

Make sure that $('#Id').val() and $('#name').val() actually have proper values.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117