5

I'm stumped as to why I am getting the error. Seems like it is just ignoring the items that I pass in. I have tried the various combinations below and a lot of others. Tried get, delete , put methods etc, no luck.

Tried Changing 'Item' and 'Key' depending on the method of course. No idea what is wrong. Primary partition key is 'uid'

'table' is a variable with the table name

let params = {
    TableName:{"S":table},
    Item:{
        uid: "2"
    }
};
let params = {
    TableName: table,
    Item:{
        uid: "2"
    }
};

docClient.query(params, function(err, data) {

Here is the full error. I think 'M' refers to an an object so not sure why that is showing up as well.

"There were 2 validation errors: * MissingRequiredParameter: Missing required key 'TableName' in >params * UnexpectedParameter: Unexpected key 'M' found in params", "code": "MultipleValidationErrors", >"errors": {"message": "Missing required key 'TableName' in params","code":"MissingRequiredParameter",time": "2020-05-09T21:51:14.530Z"}, {message": "Unexpected key 'M' found in params", "code": "UnexpectedParameter", "time": "2020-05-09T21:51:14.530Z"} ],"time": "2020-05-09T21:51:14.530Z"\"}"

Here is the lambda function

var AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient('us-east-1');

exports.handler = async event => {
    var tables = ['holdings','trades','userDetails'];
    var table = 'userDetails'//tables[2]
    var out = ''
    var params = {
        TableName: table,
        Item: {
            "uid":  2
        }
    };
    docClient.put(params, function(err, data) {
       if (err) {
           console.log("Error", err)
       } else {
           console.log("PutItem succeeded")
       }
    });
    let responseBody = {
            message: 'Test',
            par: params,
            input: out

    };

    let response = {
            statusCode: 200,
            headers: {
                "x-custom-header" : "Testing delete function"
            },
            body: JSON.stringify(responseBody)
    };

    return response
};

2 Answers2

3
var AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'})
var docClient = new AWS.DynamoDB.DocumentClient();

Added the AWS.config line, and removed the region from 'AWS.DynamoDB.DocumentClient(region)'

And then I also had to do this,

await  docClient.put(ddbparams, function(err, data) {

Added an await to this line so that the lamba would wait for the DynamoDb query to execute before returning.

0

There could be a few different things causing this issue, would help to see a bit more of your code (e.g. could have a scope issue as your using let for params assignment).

However, there is a example that uses dynamodb query as per your code in the AWS Docs. In your snippet it also seems your mixing up different API types, which have different requirements.

Although, if you wanted to do a simple put operation instead (which might be a good starting point); using your snippet you should be able to do something like:

var params = {
    TableName: table,
    Item: {
        "uid":  2
    }
};

docClient.put(params, function(err, data) {
   if (err) {
       console.log("Error", err)
   } else {
       console.log("PutItem succeeded")
   }
});
pkarfs
  • 993
  • 7
  • 19
  • Thanks for the reply, I tried that but still get the same error for some reason. I will update it with the full lambda function. – Conor McDonald May 10 '20 at 13:21
  • Hmm, how are you triggering the lambda? The fact your error mentions time as a parameter is strange. And have you stepped through your code snippet with prints to ensure the put is triggering the error? Also could you change the params variable to ddbparams or some other name to double check that particular variable is whats being referenced in the error. Seems silly but your code does look fine as far as the dynamo call is concerned. – pkarfs May 10 '20 at 15:57
  • Thanks for getting back to me. I've tried triggering the lamba through the lambda test function, API gateway test function, and by sending a POST to the endpoint. Same result for each. Yeah it is definitely the put ( well the function) triggering the error. I changed the variable name as suggested. It error is the same (params in output.) I assume params is the name of the argument. I'm so confused as to what's wrong. – Conor McDonald May 10 '20 at 22:10
  • Just to confirm, when you changed the params variable, the error that's specified still references 'params' or the new variable name? If it still references 'params' then the error most likely is in your trigger event and response handle not the function, have you checked your dynamo table to see if the item exists? – pkarfs May 10 '20 at 22:16
  • Yes, the variable is now ddbparams, the error still references 'params'. The item doesn't exist in the table no. Any idea what could be wrong with the trigger? I can send data in the POST and access it in the lambda no problem. 'exports.handler = async event => {' is what you mean by response handler? Any ideas what else to try. Thanks a lot for your help so far. – Conor McDonald May 11 '20 at 17:32
  • It works! I added 'AWS.config.update({region: 'us-east-1'})' instead of passing it int when initialising the Document client function. You were right about the async as well, had to add an 'await' to the docClient function call. Thank you so much for your help ! – Conor McDonald May 12 '20 at 07:33
  • Not a problem was happy to help out, glad you got to a working solution in the end :) – pkarfs May 12 '20 at 08:26