【问题标题】:Wy do I get an error when issuing QueryCommand in nodejs sdk v3?为什么在 nodejs sdk v3 中发出 QueryCommand 时会出现错误?
【发布时间】:2022-04-28 08:25:21
【问题描述】:

我正在使用 aws node.js sdk v3。我想使用 dynamodbdocumentclient 对表进行简单查询。我只想测试查询是否适用于 aws 开发人员指南提供的简单音乐表。

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

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

但是当我调用它时,我得到:

TypeError: Cannot read property '0' of undefined

当我做一个简单的时候:

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

我得到了正确的回应:

            {
                "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.

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb dynamodb-queries


    【解决方案1】:

    我刚遇到同样的问题,根据this guideExpressionAttributeValues 必须遵循 DynamoDB JSON 模式,因此,如果您的 artist 是字符串,则应该是:

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

    添加这个,对我有用。

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多