繁体   English   中英

尝试放置新项目时出现 DynamoDB InvalidParameterType 错误

[英]DynamoDB InvalidParameterType Error While Trying To Put A New Item

我正在尝试实现一个基于 DynamoDB 的应用程序来存储一些请求数据。 我阅读了 DynamoDB 官方文档,目前我正在按照这个官方教程进行一些基本操作。

我正在使用本地 DynamoDB docker 容器。 你可以用这个运行它:

docker run -d -p 8000:8000 amazon/dynamodb-local:latest -jar DynamoDBLocal.jar -sharedDb

当我尝试按照上面给出的教程创建一个新表时,我没有收到任何错误,一切都很好:

var params = {
TableName: 'book',
KeySchema: [
    {
        AttributeName: 'title',
        KeyType: 'HASH',
    }
],
AttributeDefinitions: [
    {
        AttributeName: 'title',
        AttributeType: 'S'
    }
],
ProvisionedThroughput: {
    ReadCapacityUnits: 1, 
    WriteCapacityUnits: 1, 
}

};
dynamodb.createTable(params, function(err, data) {
    if (err) print(err); // an error occurred
    else print(data); // successful response

});

但是当我尝试在其中放入一些新项目时:

var params = {
TableName: 'book',
Item: { // a map of attribute name to AttributeValue
    title: "Sample Application: CloudList",
    chapter: 10
    }
};

dynamodb.putItem(params, function(err, data) {
    if (err) print(err); // an error occurred
    else print(data); // successful response
});

我收到了这个错误:

在此处输入图像描述

31 验证错误实际上等于标题中的字符数: Sample Application: CloudList DynamoDB shell 也无法识别上述教程中给出的打印 function。 所以我不得不用 ppJson function 替换它。 我在哪里做错了,如何通过 Web Shell 从 DynamoDB 放置/删除/获取项目? (也可以通过 PHP 代码)


编辑:我也尝试了 Vikdor 在评论中所说的,似乎我摆脱了那个UnexpectedParameter错误,但这次我得到了Invalid attribute value type错误。

var params = {
    TableName: 'book',
    Item: { // a map of attribute name to AttributeValue
        'title': {S: "Sample Application: CloudList"},
        'chapter': {N: '10'}
    }
};

文档解释了传递给 API 的paramsItem键的结构,您的params应如下所示:

var params = {
    TableName: "book",
    Item: { 
        "title": {"S": "Sample Application: CloudList"},
        "chapter": {"N": :10"}
    }
};

请注意,即使是数字也应该用引号引起来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM