Search code examples
amazon-dynamodbdynamodb-queries

DynamoDB updating fails with validation exception


I'm using Dynamodb with the DynamoDB npm package and when I want to update data inside my Warehouse table I get the following error. It will update my data but suddenly crashes with a nonsense error. What's the problem?

ValidationException: Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :name

Code:

let updateData = {
  customer: 'mrpen',
  name: warehouseInfo.name,
  street1: warehouseInfo.street1 ? warehouseInfo.street1 : 'N/A',
  street2: warehouseInfo.street2 ? warehouseInfo.street2 : 'N/A',
  city: warehouseInfo.city ? warehouseInfo.city : 'N/A',
  state: warehouseInfo.state ? warehouseInfo.state : 'N/A',
  country: warehouseInfo.country,
  zip: warehouseInfo.zip ? warehouseInfo.zip : 'N/A',
  contactName: warehouseInfo.contactName ? warehouseInfo.contactName : 'N/A',
  contactEmail: warehouseInfo.contactEmail ? warehouseInfo.contactEmail : 'N/A',
  contactPhone: warehouseInfo.contactPhone ? warehouseInfo.contactPhone : 'N/A',
  inventoryName: data[0].attrs.inventoryName,
};
const res = Warehouse.update(updateData, (err, res) => {
   if (err) {
    throw new Error('update warehouse failed!');
   } else {
    console.log(res);
    return true;
  }
});

Solution

  • The name attribute is one of the reserved words in DynamoDB. You can either rename the attribute or define an expression attribute name to use in the place of the reserved word.

    More info on expression attributes in the docs.