shou2017.com
JP

AWS SDK3 nodejs error: dynamodb cannot read property 0 of u

Sat Oct 23, 2021
Sat Aug 10, 2024
AWS

Recently, I’ve been updating AWS SDK for nodejs to version 3, and I encountered an error, so I’m making a note of it.

In v2, you would write DynamoDB processing like this:

var params = {
  TableName: 'Table',
  IndexName: 'Index',
  KeyConditionExpression: 'HashKey = :hkey and RangeKey > :rkey',
  ExpressionAttributeValues: {
    ':hkey': 'key',
    ':rkey': 2015
  }
};

var documentClient = new AWS.DynamoDB.DocumentClient();

documentClient.query(params, function(err, data) {
   if (err) console.log(err);
   else

I thought it would be the same in v3, so I wrote it like this, but got an error:

TypeError: Cannot read property '0' of undefined

const params = {
  TableName: 'Table',
  IndexName: 'Index',
  KeyConditionExpression: 'HashKey = :hkey and RangeKey > :rkey',
  ExpressionAttributeValues: {
    ':hkey': 'key',
    ':rkey': 2015
  }
};
const command = new QueryCommand(params);
const data = await dynamoDBClient.send(command);

It seems that starting from v3, when passing values, you need to specify the type and pass an object.

So, I修正します this way.

const params = {
  TableName: 'Table',
  IndexName: 'Index',
  KeyConditionExpression: 'HashKey = :hkey and RangeKey > :rkey',
  ExpressionAttributeValues: {
    ':hkey': { S: 'key'},
    ':rkey': { N: '2015'}
  }
};
const command = new QueryCommand(params);
const data = await dynamoDBClient.send(command);

Reference Link

See Also