【问题标题】:Attribute name is a reserved keyword in DynamoDB属性名称是 DynamoDB 中的保留关键字
【发布时间】:2018-04-30 08:12:53
【问题描述】:

从 DynamoDB 读取值时,我收到列名是保留关键字的错误。我正在尝试读取名为“状态”的列的值。

我的脚本:

<script>
    AWS.config.update({
      region: "us-west-2",
      endpoint: 'https://dynamodb.us-west-2.amazonaws.com',
      accessKeyId: "Fakeaccesskey",
      secretAccessKey: "Fakesecretkey"
    });

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

    function readItembedonestatus() {
        var table = "TA_Latest_Log";
        var GWID = "BB00000001";
        var ID = "AA00000013";

        var params = {
            TableName: table,
            Key:{
                "GWID": GWID,
                "ID": ID
            },
               "ProjectionExpression": "#node_status"
               "ExpressionAttributeNames": "#node_status": "Status"
        };
        docClient.get(params, function(err, data) {
            if (err) {
                document.getElementById('bedonestatus').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
            } else {
                document.getElementById('bedonestatus').innerHTML = data.Item.#node_status + "&deg;";

            }
        });
    }

</script>

我添加了 ExpressionAttributeNames,但是我无法让它工作。上面的脚本什么也不返回。

【问题讨论】:

    标签: javascript html amazon-web-services amazon-dynamodb


    【解决方案1】:
    <script>
        AWS.config.update({
          region: "us-west-2",
          endpoint: 'https://dynamodb.us-west-2.amazonaws.com',
          accessKeyId: "Fakeaccesskey",
          secretAccessKey: "Fakesecretkey"
        });
    
        var docClient = new AWS.DynamoDB.DocumentClient();
    
        function readItembedonestatus() {
            var table = "TA_Latest_Log";
            var GWID = "BB00000001";
            var ID = "AA00000013";
    
            var params = {
                TableName: table,
                Key:{
                    "GWID": GWID,
                    "ID": ID
                },
                ExpressionAttributeNames: {"#node_status":"Status"},
                ProjectionExpression: "#node_status"
            };
            docClient.get(params, function(err, data) {
                if (err) {
                    document.getElementById('bedonestatus').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
                } else {
                    document.getElementById('bedonestatus').innerHTML = data.Item.#node_status + "&deg;";
    
                }
            });
        }
    
    </script>
    

    ExpressionAttributeNames 必须是一个映射。而且您的参数列表中缺少一个逗号..

    【讨论】:

    • 感谢您的回答,但还是一样。它没有返回任何东西
    • @Stu 将其作为新回复发布,因为这不允许在 cmets 中正确格式化。
    • 很可能没有匹配的项目或项目没有名为状态的属性。如果没有错误消息或您要编辑的有效查询,我无法为您提供帮助。
    • 状态在那里,下面我发布了一个类似的工作查询。它只是没有保留关键字。
    • 好的,我明白了。您可以发布您要查找的整个项目(值)吗?
    【解决方案2】:

    以下查询有效,但这是因为它没有保留关键字 (BatV)。

    <script>
        AWS.config.update({
          region: "us-west-2",
          endpoint: 'https://dynamodb.us-west-2.amazonaws.com',
          accessKeyId: "Fakeaccesskey",
          secretAccessKey: "Fakesecretkey"
        });
    
        var docClient = new AWS.DynamoDB.DocumentClient();
    
        function readItemkitchenbat() {
            var table = "TA_Latest_Log";
            var GWID = "BB00000001";
            var ID = "AA00000029";
    
            var params = {
                TableName: table,
                Key:{
                    "GWID": GWID,
                    "ID": ID
                },
                   "ProjectionExpression": "BatV"
            };
            docClient.get(params, function(err, data) {
                if (err) {
                    document.getElementById('kitchenbat').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
                } else {
                    document.getElementById('kitchenbat').innerHTML = data.Item.BatV + "V";
                    
                }
            });
        }
    
    </script>
    

    这是亚马逊关于保留关键字的文档:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2017-07-06
      • 2020-03-31
      • 2020-03-12
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多