2

I am using aws node.js sdk v3. I want to do a simple query on the table using dynamodbdocumentclient. I wanted to do just a test if query works on a simple music table which is provided by aws developer guide.

const data = await this.dynamoDBDocumentClient.send(new QueryCommand({
      TableName: "Music",
      KeyConditionExpression: "#Artist = :artist",
      ExpressionAttributeName: {
        "#Artist": "artist"

      },
      ExpressionAttributeValues: {
        ":artist": "Acme Band"
      },
    });

But when I call this I get:

TypeError: Cannot read property '0' of undefined

When I do a simple:

const data = await.this.dynamoDBDocumentClient.send(newScanCommand({TableName: "Music"});

I am getting the correct response:

            {
                "AlbumTitle": {
                    "S": "Somewhat Famous"
                },
                "Awards": {
                    "N": "1"
                },
                "Artist": {
                    "S": "No One You Know"
                },
                "SongTitle": {
                    "S": "Call Me Today"
                }
            },
            {
                "AlbumTitle": {
                    "S": "Songs About Life"
                },
                "Awards": {
                    "N": "10"
                },
                "Artist": {
                    "S": "Acme Band"
                },
                "SongTitle": {
                    "S": "Happy Day"
                }
            },
            {
                "AlbumTitle": {
                    "S": "Songs About Sadness"
                },
                "Awards": {
                    "N": "8"
                },
                "Artist": {
                    "S": "Acme Band"
                },
                "SongTitle": {
                    "S": "Sad Day"
                }
            }
        ],
        "ScannedCount": 3
    }```

What am I doing wrong? I don't get why I am getting en error.

fewfew
  • 231
  • 2
  • 10

2 Answers2

1

I just had the same problem, and according to this guide, the ExpressionAttributeValues must follow the DynamoDB JSON schema, being so, if your artist is a string, it should be:

...
ExpressionAttributeValues: {
    ":artist": {S: "Acme Band"}
},
...

Adding this, worked for me.

dnarvaez27
  • 472
  • 1
  • 6
  • 10
0

I think you simply have a typo here:

ExpressionAttributeName

It should be ExpressionAttributeNames (missing last letter).

However AWS-SDK is not very helpful with error messages. Use TypeScript to avoid similar issues, since it will show you, that you are using wrong field name basing on types definition.

PatrykMilewski
  • 922
  • 8
  • 17