Lucas_Santos
Lucas_Santos

Reputation: 4740

UpdateItem in DynamoDB error ValidationException: Invalid attribute value type

I have a table called JuridicalPerson in my DynamoDB

var params = {
  AttributeDefinitions: [{
    AttributeName: 'code',
    AttributeType: 'S'
  }],
  KeySchema: [{
    AttributeName: 'code',
    KeyType: 'HASH'
  }],
  ProvisionedThroughput: {
    ReadCapacityUnits: 5,
    WriteCapacityUnits: 5
  },
  TableName: 'JuridicalPerson'
}

I can save items there, but I can't update those items.

Sample of item's in my JuridicalPerson table

{
   "code": {
     "S": "jp_rJaHvVrzf"
   },
   "status": {
     "S": "pending"
   }
}

Update Expression

function updateDynamoDB (payload) {
  const adhesionUpdate = Object.assign({}, payload)

  return new Promise((resolve, reject) => {
    const params = {
      TableName: 'JuridicalPerson',
      Key: {
        'code': {
          'S': adhesionUpdate.code
        }
      },
      UpdateExpression: 'SET #status = :val1',
      ExpressionAttributeNames: {
        '#status': 'status'
      },
      ExpressionAttributeValues: {
        ':val1': { 'S': adhesionUpdate.status }
      },
      ReturnValues: 'ALL_NEW'
    }

    return dynamoAdapter.getState().update(params, (err, items) => {
      if (err) {
        return reject(err)
      }
      return resolve(items)
    })
  })
}

If I put a console.log before the Update just to see the params, we have

params:  { TableName: 'JuridicalPerson',
  Key: { code: { S: 'jp_rJaHvVrzf' } },
  UpdateExpression: 'set #status = :val1',
  ExpressionAttributeNames: { '#status': 'status' },
  ExpressionAttributeValues: { ':val1': { S: 'active' } },
  ReturnValues: 'ALL_NEW' }

But I got the following error

err:  { ValidationException: Invalid attribute value type
  message: 'Invalid attribute value type',
  code: 'ValidationException',
  time: 2017-12-18T12:40:39.488Z,
  requestId: 'bc23aab1-d9a5-426f-a1af-3ff558e7e0fa',
  statusCode: 400,
  retryable: false,
  retryDelay: 41.054909592801195 }

Upvotes: 1

Views: 3522

Answers (1)

CoderBoy
CoderBoy

Reputation: 41

I had the same ambiguous error message when using the DynamoDb Client rather than the DynamoDb Resource.

The DDB Client accepts definitions of values in short form key: value rather than explicit type form accepted by the DDB resource key: {'S': value}.

In your example try using:

const params = {
  TableName: 'JuridicalPerson',
  Key: {
    'code': adhesionUpdate.code
  },
  UpdateExpression: 'SET #status = :val1',
  ExpressionAttributeNames: {
    '#status': 'status'
  },
  ExpressionAttributeValues: {
    ':val1': adhesionUpdate.status
  },
  ReturnValues: 'ALL_NEW'
}

Upvotes: 4

Related Questions