简体   繁体   English

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

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

I am trying to implement a DynamoDB based application to store some request data.我正在尝试实现一个基于 DynamoDB 的应用程序来存储一些请求数据。 I read the DynamoDB official documentation and currently I am following this official tutorial to do some basic operations.我阅读了 DynamoDB 官方文档,目前我正在按照这个官方教程进行一些基本操作。

I am using a local DynamoDB docker container.我正在使用本地 DynamoDB docker 容器。 You can run it with this:你可以用这个运行它:

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

When I try to create a new table just same as the tutorial I gave above, I got no error, everything is fine:当我尝试按照上面给出的教程创建一个新表时,我没有收到任何错误,一切都很好:

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

});

But when I try to put some new items in it:但是当我尝试在其中放入一些新项目时:

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
});

I got this error:我收到了这个错误:

在此处输入图像描述

31 validation error actually is equal to number of chars in title: Sample Application: CloudList . 31 验证错误实际上等于标题中的字符数: Sample Application: CloudList DynamoDB shell also did not recognized the print function which is given in the above tutorial. DynamoDB shell 也无法识别上述教程中给出的打印 function。 So I had to replace it with ppJson function.所以我不得不用 ppJson function 替换它。 Where am I doing wrong and how can I put/delete/get items from DynamoDB via Web Shell?我在哪里做错了,如何通过 Web Shell 从 DynamoDB 放置/删除/获取项目? (and also via PHP code) (也可以通过 PHP 代码)


Edit: I also tried what Vikdor said in the comment, it seems I got rid of that UnexpectedParameter error but this time I got Invalid attribute value type error.编辑:我也尝试了 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'}
    }
};

This doc explains the structure of the Item key in the params passed to the API and your params should be as follows:文档解释了传递给 API 的paramsItem键的结构,您的params应如下所示:

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

Note that even the numbers should be enclosed in quotes.请注意,即使是数字也应该用引号引起来。

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

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