2

I want to write a function that updates given parameter in dynamodb.

For example in a dynamodb table where each userId is the key I have values like

{ "categoryname": "a", "skillState": "a", "skipcount": 1, "userId": "amzn1.ask.account.xxx” }

I wanna set the "categoryname": "b" although there might be 10-15 fields like this so I dont wanna hard code the field name.

function (userId,itemToUpdate,itemValue,callback) {


    var updateExpressionString = "SET #"+itemToUpdate+" =:val1";
    var expressionAtt = '#'+itemToUpdate + '';

    console.log(updateExpressionString)
    console.log(expressionAtt)

    this.dynamodb.updateItem({
            TableName: constants.dynamoDBDetailTableName,
            Key: {
                userId: {
                    S: userId
                }
            },
            UpdateExpression: updateExpressionString,
            ExpressionAttributeNames : {
                   expressionAtt : itemToUpdate
            },
            ExpressionAttributeValues : {
                ':val1': {'S':itemValue}
            }
        }, function (err, data) {
            if (err) {
                console.log(err)
                console.log('Error ')
            } else if (data.Item === undefined) {

            }else {
                  console.log(data)  
            }
    });
}

In ExpressionAttributeNames:

{ ValidationException: ExpressionAttributeNames contains invalid key: Syntax error; key: "expressionAtt"

This throws error obviously thinking that expressionAtt is the key while it is a local variable.

I am new to node.js , how can pass the local variable in to ExpressionAttributeNames and ExpressionAttributeValues

1 Answer 1

3

One way of dealing with this could be to pull the object out of updateItem, put it into its own variable like so:

var item = {
    TableName: constants.dynamoDBDetailTableName,
    Key: {
        userId: {
            S: userId
        }
    },
    UpdateExpression: updateExpressionString,
    ExpressionAttributeNames: {},
    ExpressionAttributeValue: {
        ':val1': {'S': itemValue}
    }
};

item.ExpressionAttributeNames[expressionAtt] = itemToUpdate;

this.dynamodb.updateItem(item);

I believe that will fix your problem

Sign up to request clarification or add additional context in comments.

Comments

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.