Moriuks
Moriuks

Reputation: 159

Conditional update to dynamoDB with nested object using node.js

I'm trying to update 3 nested items in a db table,my lambda runs fine and doesnt give any erros,but when a query the table it doesnt show the new values,i´m not sure if im invoking the table or passing the arguments correctly

my partion key/primary key is badgeNumber

my dynamoDB table looks like this: (the items i'm trying to update are date,hour,register to yyy-mm-dd,hh-mm-ss and true

{
  "assistance": [
    {
      "date": "null",
      "hour": "null",
      "register": false
    }
  ],
  "badgeNumber": "0000",
  "data": {
    "cardType": "elem",
    "firstName": "Moriuks",
    "imageURL": "url",
    "lastName": "Mora",
    "position": "Student"
  }
}

the condition to update the items is if register is = false then write the new values to the table.

my code looks like this pppp

var updateAsisstance = function(day,hour,id){
            var docClient = new AWS.DynamoDB.DocumentClient();

            var params = {
            TableName:"someTable",
            Key: { badgeNumber : 0000 },
            UpdateExpression: "SET #asi[0].#reg  = :locVal",
            ExpressionAttributeNames: {
              '#asi': 'asisstance',
              '#reg': 'register',
            },
            ConditionExpression: "NE(#asi[0].#reg:true)",
            ExpressionAttributeValues:{
                ":date":day,
                ":hour":hour,
                ":locVal":true
            },
            ReturnValues:"UPDATED_NEW"
        };
            docClient.update(params, function(err, data) {
          if (err) {
            console.log("Error", err);
          } else {
            console.log("Success", data);
          }
        });

        };

after defining the funcion,im calling it using

updateAssistance(day,hour,id)

the expected output should look something like this:

  "assistance": [
    {
      "date": "yyyy-MM-DD",
      "hour": "HH-MM-SS",
      "register": true
    }
  ],

Upvotes: 1

Views: 1934

Answers (1)

Moriuks
Moriuks

Reputation: 159

i solved it changing the code,also,my condition expression was wrong...here is what it looks like.

'use strict';
const AWS = require('aws-sdk');

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var hour = (today.getHours()-5) + ":" + today.getMinutes() + ":" + today.getSeconds();

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

    const documentClient = new AWS.DynamoDB.DocumentClient();
    let responseBody = "";
    let statusCode = 0;

    var params = {
    TableName:"SomeTable",
    Key: { badgeNumber : '0000' },
    UpdateExpression: "set assistance[0].register = :n,assistencia[0].date = :date,assistencia[0].hour = :hour",
    ExpressionAttributeNames: {
      '#asi': 'assistance',
      '#reg': 'register'
    },
    ConditionExpression: "(#asi[0].#reg = :p)",
    ExpressionAttributeValues:{
        ":n":true,
        ":p":false,
        ":date":date,
        ":hour":hour
    },
    ReturnValues:"UPDATED_NEW"
    }


    try {
        const data = await documentClient.update(params).promise();
        responseBody = JSON.stringify(data);
        statusCode = 204;
    } catch (err) {
        responseBody = `Unable to update product: ${err}`;
        statusCode = 403;
    }    

    const response = {
        statusCode: statusCode,
        headers: {
            "Content-Type": "application/json"
        },
        body:responseBody
    }
    return response
}

this changes the nested values inside my dynamoDB table,if you dont have any its pretty straight forward.

Upvotes: 2

Related Questions