Lambda DynamoDB The parameter cannot be converted to a numeric value

0 votes

Can anyone figure out why this number string cannot be converted to a number!!

I have the following Lambda function: taking a set of data being sent from API Gateway and then configuring it to save it to DynamoDB.

// Load the AWS SDK for Node.js. var AWS = require("aws-sdk"); // Set the AWS Region. AWS.config.update({ region: "us-east-1" }); // Create DynamoDB service object. var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); exports.handler = async (event) => { // response variables let body; let statusCode = "200"; const headers = { "Content-Type": "application/json", }; // request parameters let params = { TableName: "users_amazon_profiles", Item: { "user_id": { "S": "event.user_id" }, "refresh_token": { "S": "event.refreshToken" }, "profile_id": { "N": "event.profileArray[0].profileId" }, "country_code": { "S": "event.profileArray[0].countryCode" }, "currency_code": { "S": "event.profileArray[0].currencyCode" }, "account_info": { "M": { "marketplaceStringId" : { "S": "event.profileArray[0].accountInfo.marketplaceStringId" }, "id" : { "S": "event.profileArray[0].accountInfo.id" }, "name" : { "S": "event.profileArray[0].accountInfo.name" }, "type" : { "S": "event.profileArray[0].accountInfo.type" }, "validPaymentMethod" : { "BOOL": event.profileArray[0].accountInfo.validPaymentMethod } } }, "daily_budget": { "N": "event.profileArray[0].dailyBudget" }, "timezone": { "S": "event.profileArray[0].timezone" }, }, }; // request try { console.log("profileId:"); console.log(event.profileArray[0].profileId); body = await ddb.putItem(params).promise(); } catch (err) { statusCode = "400"; body = err.message; } // dynamic response return { statusCode, body, headers, }; };

And here is the JSON test object being used for testing:

{ "user_id": "g490754-49b9-9079-c63aba5cccf6", "refreshToken": "Atzr|IwEBIMvPPWsTFHLgvbEhgUn30P57Kake_nfIgVa-6GJeIe9vsW38GdoDcpzCXIlqd6-82YdC8JISNxuQJQ5QREGmSX0YRu6qJ4zteznkqoaygCPk1ZqHE2jPhcbDuDZiGRNOWpis0pPpKaS9ylJsp2Vquwa5lM6Olu_qhRanp8iewCLmK-k4W8FkjA0aiiCqJPyfb7dsloXO-TGDPK8WXSoLREnzhucaO5Xn-TnwicZzKrq0mYBHyhzm1XNvEGmSiDm1cxkgFcQr0TvkVhyOoTeLuKz3Cw-1iAQg6rbaXiEvgo18ganZYO71vpv4jy5S91h5NVZwCgtG7tJ73A5DzOBdRXjjhke5xJ7HHEZQncQUPjE_R0gZBtsXpKfJVaaPjY925m-GCMbdhe9aFR0gKYlaDz-q-Nbo_I-vUA", "profileArray": [ { "accountInfo": { "marketplaceStringId": "OKE9KIKX0DER", "id": "OI9FU2GLDTTWKS", "name": "Test Seller", "type": "seller", "validPaymentMethod": true }, "countryCode": "US", "currencyCode": "USD", "dailyBudget": "999999999", "profileId": "2591184040605621", "timezone": "America/Los_Angeles" }, { "accountInfo": { "marketplaceStringId": "ATVERKIKX0DER", "id": "A3UJTRELDTTWKS", "name": "ACME", "type": "vendor", "validPaymentMethod": false }, "countryCode": "US", "currencyCode": "USD", "dailyBudget": "10000", "profileId": "999918498705620", "timezone": "America/Los_Angeles" }, { "accountInfo": { "marketplaceStringId": "ATVOIKIKX0DER", "id": "A3UJU6HLDTTWKS", "name": "Woolworths", "type": "seller", "validPaymentMethod": true }, "countryCode": "US", "currencyCode": "USD", "dailyBudget": "999999999", "profileId": "111184040605621", "timezone": "America/Los_Angeles" } ] }

So basically in the Lambda function, we are looking at the profile array which has three objects in it, and we are taking data out of the first object and putting it into the params that we are going to save to DynamoDB.

The items specifically that we are having trouble with is:

"profile_id": { "N": "event.profileArray[0].profileId" },

We can see that we are specifying this piece of data to be a number, and we are inputting it as a string (as we should be) and in the test JSON object, we can see that it is a string. However, when we run this function we get the response:

{ "statusCode": "400", "body": "The parameter cannot be converted to a numeric value: event.profileArray[0].profileId", "headers": { "Content-Type": "application/json" } }

This is crazy because that item is a number. I have tried everything I can think of. You can see in the request I console logged the item just to make sure it is coming in correctly and yes, we get the following in the console log:

2591184040605621

I thought the problem was that the profile_id is the sort key of the DynamoDB table, so I made sure that the data type of this sort key is a number, so that is not the issue.

Can anyone figure out why this number string cannot be converted to a number!!

Apr 29, 2022 in AWS by Soham
• 9,710 points

edited Mar 4 5 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP