0

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

1 Answer 1

1

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

"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",
Sign up to request clarification or add additional context in comments.

2 Comments

Right, so it's complaining about Syntax error In the Dynamo query. I think it might be the double comma here: :wC, , ratherClick
Can you tell last thing when i am trying to test it on codepen its showing same syntax error ? I have added code in my question of codepen in end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.