2
votes

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

2 Answers

1
votes
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
votes

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")
   }
});