Closed SagarMhatre closed 6 years ago
Further, when I dig into the "One or more parameter values were invalid: Missing the key id in the item" issue, I tried setting up my own Lambda Function with the below nodejs code
exports.dynamodb_handler = function(event, context, callback) {
console.log('Event' + JSON.stringify(event.body));
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'ap-south-1'});
// Create DynamoDB document client
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
var params = {
TableName: 'employee',
Item : JSON.parse(event.body)
};
// Call DynamoDB to add the item to the table
docClient.put(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
var responseBody = {
message: "hello",
input: event
};
// The output from a Lambda proxy integration must be
// of the following JSON object. The 'headers' property
// is for custom response headers in addition to standard
// ones. The 'body' property must be a JSON string. For
// base64-encoded payload, you must also set the 'isBase64Encoded'
// property to 'true'.
var response = {
statusCode: 201,
headers: {
"x-custom-header" : "my custom header value"
},
body: JSON.stringify(responseBody)
};
console.log("response: " + JSON.stringify(response))
callback(null, response);
}
Here, if we send the request through the API Gateway as
{
"id" : "sagar16",
"image" : "sagar.jpg",
"age":32
}
it works fine & makes an entry to the table. If we call the same using the Lambda Web Console Function Testing
{
"body": {
"id": "sagar19",
"image": "sagar.jpg",
"age": 20
}
}
It gives an error SyntaxError: Unexpected token o in JSON at position 1
Now, If we simply change the below in our lambda code (Remove the JSON.parse at the params.Item before the put)
var params = {
TableName: 'employee',
Item : event.body
};
it works fine with the Lambda Web Console Testing , but now if we try it with the above API Request, the cloudwatch logs show the below error
Error { ValidationException: One or more parameter values were invalid: Missing the key id in the item
Edited by: MhatreSagar on Mar 31, 2018 7:27 AM
Hi @SagarMhatre which version of the cli are you using?
1.0.16
Hi @SagarMhatre I did the same steps (macOS Sierra). I had awsmobile-cli@1.0.16. Which OS are you using?
Windows 10
But I do not think that it maybe a OS issue since the lambda function when called directly from the web ui also gives an error
I will reproduce this on windows 10 and I will let you know.
Thanks!
I have tested it with awsmobile 1.1.4 & it worked.
The error on the Mobile Hub Console (Testing employeeCRUD) by sending a post request to /employee with body* still stays, but I think it is because my API is a secured one & requires login.
I have followed the doc at [https://docs.aws.amazon.com/aws-mobile/latest/developerguide/web-access-databases.html] (Access Your Database) & created an employee table & cloud-api.
Now, when I invoke
I get the error
I have also tried it from the Mobile Hub Console (Testing employeeCRUD) by sending a post request to /employee with body*
But I get the response
Even a GET request to /employee from the Mobile Hub Web Console yields: