Shahnaz Raheem
Shahnaz Raheem

Reputation: 228

AWS Lambda update function showing ValidationException

I am trying to update data by lamda function. I just created a PUT Method and add lambda function in method after reading documentation i created a function here is the code

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});

AWS.config.update({region: 'us-east-2', apiVersion: '2012-08-10'});

var docClient = new AWS.DynamoDB.DocumentClient();

exports.handler = (event, context, callback) => {

    const params = {
    TableName: "Would-You-Rather",
    Key:{
        "QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",
    },
    UpdateExpression: "set would = :w, rather = :r, wouldClick = :wC, , ratherClick = :rC ",
    ExpressionAttributeValues:{
        ":w": "Working",
        ":r": "Working",
        ":wC": 12,
        ":rC": 1
    },
    ReturnValues:"UPDATED_NEW"
};

console.log("Updating the item...");
docClient.update(params, function(err, data) {
    if (err) {
        console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
    }
});

};

But when I am testing the function it's showing

   "message": "Invalid UpdateExpression: Syntax error; token: \",\", near: \", , ratherClick\"",
  "code": "ValidationException",

If i wrap ExpressionAttributes like this

ExpressionAttributeValues:{
    ":w": "Working",
    ":r": "Working",
    ":wC": "12",
    ":rC": "1"
},

Then its showing this error

  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Invalid or unexpected token",

This is my POST method which is working fine add this because maybe it will help in better understanding

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const params = {
        Item: {
            "QuestionID": {
                S: context.awsRequestId
            },
            "Would": {
                S: event.would
            },
            "Rather": {
                S: event.rather
            },
            "wouldClick": {
                N: event.wouldClick
            },
            "ratherClick": {
                N: event.ratherClick
            }
        },
        TableName: "Would-You-Rather"
    };
    dynamodb.putItem(params, function(err, data) {
        if (err) {
            console.log(err);
            callback(err);
        } else {
            console.log(data);

            callback(null, data);
        }
    });
};

Codepen code

var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather');
xhr.onreadystatechange = function(event) {
  console.log(event.target.response);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');

xhr.send(JSON.stringify({Would: Coffe, Rather: Tea, wouldClick: 15, ratherClick: 13, QuestionID: 3e8976be-e763-4c69-84c3-be03ec4a38de}));

Upvotes: 0

Views: 1248

Answers (1)

Tomasz
Tomasz

Reputation: 717

There's a syntax error. You forgot to put the closing quote here

"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",

Upvotes: 1

Related Questions