Lambda Function dynamodb


Hello, I am learning lambda function. I am using nodejs16.x runtime and aws-sdk to perform CRUD operation in dynamodb tables. I am using aws management console to this. I created a table in dynamodb the table has partition key as id then i inserted an item into it that looks like this {"id" : {S : "someid"}, "views" : {N : "0"}} . Now using lambda function I am updating this item's view property; for that I am incrementing the views by 1. after running the function the returned object is correct and show the new values of views for that item as 1. But when I open the dynamodb console and see the table, the views property value is not 1 but 2. And therefore on 2nd execution --> return {views : 3} where as this should be 2 but in dynamodb console the value is now at 4.

lambda function Enter image description here

Dynamodb Table Enter image description here

executing lambda function Enter image description here

dyamodb table item -- now view should be 1 but it shows 2 Enter image description here

  • first decouple the variable like this,'/submit', function(req, res) {
        // console.log(req.body)
            ip: req.body.userIp
        }, function(err, payload) {
            // console.log(payload)
            var input = {
                "Created_on": new Date().toString(),
                "Subject": req.body.subject,
                "Message": req.body.message,
                "UserInfo": JSON.stringify(payload)
            var params = {
                TableName: Leads_table_name,
                Item: input
            docClient.put(params, function(err, data) {
                if (err) {
                    console.log("users::save::error - " + JSON.stringify(err, null, 2));
                } else {
        res.sendFile('public/response.html', {
            root: __dirname
feita há 2 anos444 visualizações
1 Resposta

I was able to reproduce your issue. Will need to do more research and ask some Node.js experts, as I am primarily from a Java background. However, following all documentation and code samples on even third-party sites like Stackoverflow, what you are doing in your code seemed to be in line. I even tried using ADD instead of SET, but the value in DynamoDb was always incrementing by 2.

profile pictureAWS
respondido há 2 anos

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas