1

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 }
3
  • I don't see any obvious error unless status is defined as a key in any secondary index, and if so of any other data type than string? Commented Dec 19, 2017 at 8:34
  • 2
    I know that the DynamoDB and the Document Client API documentation has sometimes been a bit ambigious in that sometimes values should not be described in the {S: 'some value'} form, but simply as 'some value'. The documentation does however clearly state that ExpressionAttributeValues should be in the more verbose form, but if I were you I'd give it a try without the {S: ...}. Personally I have only been using the DocumentClient API since its release, mostly to get rid of the incredibly verbose syntax with all these S: and N:... Commented Dec 19, 2017 at 8:48
  • One small thing. Shouldn't your condition at the end have an 'else' (as in return an error else the results). Also, im not a javascript programmer, but there are a lot of return statements in there and its hard to see the flow of the code. Are you sure which code is being executed? Other than that I couldn't spot any problems at all in the actual DynamoDB parts. Commented Dec 19, 2017 at 9:23

1 Answer 1

4

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'
}
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.