Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
How to do update in AWS Dynamo DB using NodeJS

I have written this function to do update in dynamo table

const updateTask = async (req, res) => {
  try {
    const { existingTaskText,updatedTaskText } = req.body;
    console.log(existingTaskText,updatedTaskText );
    UPDATE({
      TableName: "todos",
      Key:{ task: existingTaskText},
      UpdateExpression:"set task = :task",
      ExpressionAttributeValues: {":task": updatedTaskText},
    });
    res.status(200).json({ data: "this is controller" });
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
};

this is calling UPDATE

const UPDATE = async (payload) => {
  try {
    console.log(payload);
    const updateDoc = await dbClient
      .update({
        TableName: payload.TableName,
        Key: payload.Key,
        UpdateExpression: payload.UpdateExpression,
        ExpressionAttributeNames:payload.ExpressionAttributeNames,
        ReturnValues: "UPDATED_NEW",
      })
      .promise();
    console.log(updateDoc);
  } catch (error) {
    console.log(error);
  }
};

When I am testing this in postman, I am getting this error

ValidationException: Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: :task

this is payload log getting passed

{
  TableName: 'todos',
  Key: { task: 'see its  done' },
  UpdateExpression: 'set task = :task',
  ExpressionAttributeValues: { ':task': 'edited' }
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I made below common functions for the update, get, and create a table.use the same.

const AWS = require('aws-sdk');
AWS.config.update({ region: "us-east-1",accessKeyId : process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY });
const dynamoDB = new AWS.DynamoDB()
const documentClient = new AWS.DynamoDB.DocumentClient();

const Dynamo = {
    async get(id, TableName) {
        const params = {
            TableName,
            Key: {
                id,
            },
        };

        const data = await documentClient.get(params).promise();

        if (!data || !data.Item) {
           throw Error(`There was an error fetching the data for ID of ${id} from ${TableName}`);
           
        }
        console.log(data);

        return data.Item;
    },
    async getall(TableName) {
        const params = {
            TableName: TableName,
          };

        const data = await documentClient.scan(params).promise();

        if (!data || !data.Item) {
            throw Error(`There was an error fetching the data for ID of ${ID} from ${TableName}`);
        }
        console.log(data);

        return data.Items;
    },
    async getMany(params) {
        const data = await documentClient.scan(params).promise();
        console.log(data);
        if (!data || !data.Items) {
            throw Error(`There was an error fetching the data`);
        }

        return data.Items;
    },

    async write(data, TableName) {
        console.log('write dynamo',data, TableName);
        if (!data.id) {
            throw Error('no ID on the data');
        }

        const params = {
            TableName,
            Item: data,
        };

        const res = await documentClient.put(params).promise();

        if (!res) {
            throw Error(`There was an error inserting ID of ${data.id} in table ${TableName}`);
        }
        console.log('res of write dynamo ',res);
        return data;
    },
    async createTable(TableName) {
    documentClient
    .scan({
            TableName: TableName,
        })
    .promise()
    .catch(error => {
  
        return new Promise(resolve => {
            dynamoDB
            .createTable({
              AttributeDefinitions: [
                {
                  AttributeName: "id",
                  AttributeType: "S",
                },
              ],
              KeySchema: [
                {
                  AttributeName: "id",
                  KeyType: "HASH",
                },
              ],
              BillingMode: "PAY_PER_REQUEST",
              TableName: TableName,
            })
            .promise()
            .then(data => console.log("Success!", data))
            .catch(console.error)
        })
      });

    },

};
module.exports = Dynamo;
about 4 years ago · Juan Pablo Isaza Denunciar

0

When you call the dbClient.update method, you are declaring the parameter ExpressionAttributeNames. It should be ExpressionAttributeValues. This is why the error message indicates that expression attribute value used in expression is not defined.

So you can try it changing the dbClient.update call in this way:

const updateDoc = await dbClient
  .update({
    TableName: payload.TableName,
    Key: payload.Key,
    UpdateExpression: payload.UpdateExpression,
    ExpressionAttributeValues:payload.ExpressionAttributeValues,
    ReturnValues: "UPDATED_NEW",
  })
  .promise();
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here as you are setting 'ExpressionAttributeNames:', you have to set 'ExpressionAttributeValues' as well.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda