Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facing this error "ValidationException: The parameter cannot be converted to a numeric value: NaN"

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

like image 511
Shreyas Anil Chaudhari Avatar asked Dec 19 '25 10:12

Shreyas Anil Chaudhari


1 Answers

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.

like image 155
Jack Marchetti Avatar answered Dec 21 '25 03:12

Jack Marchetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!