错误 InvalidParameterType:预期 params.Item['pid'] 是 DynamoDB 中的结构
Error InvalidParameterType: Expected params.Item['pid'] to be a structure in DynamoDB
注意:所有这些都发生在 DynamoDB 的本地实例上。
这是我用来从 DynamoDB Shell 创建 table 的代码:
var params = {
TableName: "TABLE-NAME",
KeySchema: [
{ AttributeName: "pid",
KeyType: "HASH"
}
],
AttributeDefinitions: [
{ AttributeName: "pid",
AttributeType: "S"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
这是被调用以将元素添加到数据库中的函数(在 node.js 中):
function(request, response) {
params = {
TableName: 'TABLE-NAME',
Item: {
pid: 'abc123'
}
};
console.log(params);
dynamodb.putItem(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
}
我得到的输出是:
{ TableName: 'TABLE-NAME',
Item: { pid: 'abc123' } } // THIS IS PARAMS
{
"message": "There were 7 validation errors:\n* InvalidParameterType: Expected params.Item['pid'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '1' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '2' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '3' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '4' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '5' found in params.Item['pid']",
"code": "MultipleValidationErrors",
"errors": [
{
"message": "Expected params.Item['pid'] to be a structure",
"code": "InvalidParameterType",
"time": "2015-11-26T15:51:33.932Z"
},
{
"message": "Unexpected key '0' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '1' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '2' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '3' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '4' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.934Z"
},
{
"message": "Unexpected key '5' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.934Z"
}
],
"time": "2015-11-26T15:51:33.944Z"
}
我不明白为什么或如何获取键 0、1、2、3、4 和 5,而它们在上一行打印时不存在。
此外,如何修复错误 Expected params.Item['pid'] to be a structure
?我已经将它声明为一个字符串,并且正在尝试存储一个字符串!
其他注意事项:
当我在 shell 上 运行 时,我在函数中使用的相同代码工作得很好。我还包含了 aws-sdk 并根据需要对其进行了配置:
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
AWS.config.endpoint = 'http://localhost:8000/'
var dynamodb = new AWS.DynamoDB();
注意:如以下多条评论所述,此答案可能不再有效。
从 nodejs 向数据库中添加记录必须使用的函数是 put
而不是 DynamoDB shell 中使用的 putItem
。将上面的函数更改为以下修复它。
function(request, response) {
params = {
TableName: 'TABLE-NAME',
Item: {
pid: 'abc123'
}
};
console.log(params);
dynamodb.put(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
}
putItem()
method on the AWS.DynamoDB
class 期望 params.Item
对象被格式化为 AttributeValue 表示。这意味着你必须改变这个:
params = {
TableName: 'TABLE-NAME',
Item: {
pid: 'abc123'
}
};
进入这个:
params = {
TableName: 'TABLE-NAME',
Item: {
pid: {
S: 'abc123'
}
}
};
如果您想使用本机 javascript 对象,您应该使用 AWS.DynamoDB.DocumentClient
class,它会自动将 Javascript 类型编组到 DynamoDB AttributeValues 上,如下所示:
- 字符串 -> S
- 数量 -> N
- 布尔值 -> 布尔值
- 空 -> 空
- 数组 -> L
- 对象 -> M
- Buffer、File、Blob、ArrayBuffer、DataView 和 JavaScript 类型数组 -> B
它提供了一个put()
method, that delegates to AWS.DynamoDB.putItem()
.
注意:所有这些都发生在 DynamoDB 的本地实例上。
这是我用来从 DynamoDB Shell 创建 table 的代码:
var params = {
TableName: "TABLE-NAME",
KeySchema: [
{ AttributeName: "pid",
KeyType: "HASH"
}
],
AttributeDefinitions: [
{ AttributeName: "pid",
AttributeType: "S"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
这是被调用以将元素添加到数据库中的函数(在 node.js 中):
function(request, response) {
params = {
TableName: 'TABLE-NAME',
Item: {
pid: 'abc123'
}
};
console.log(params);
dynamodb.putItem(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
}
我得到的输出是:
{ TableName: 'TABLE-NAME',
Item: { pid: 'abc123' } } // THIS IS PARAMS
{
"message": "There were 7 validation errors:\n* InvalidParameterType: Expected params.Item['pid'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '1' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '2' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '3' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '4' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '5' found in params.Item['pid']",
"code": "MultipleValidationErrors",
"errors": [
{
"message": "Expected params.Item['pid'] to be a structure",
"code": "InvalidParameterType",
"time": "2015-11-26T15:51:33.932Z"
},
{
"message": "Unexpected key '0' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '1' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '2' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '3' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.933Z"
},
{
"message": "Unexpected key '4' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.934Z"
},
{
"message": "Unexpected key '5' found in params.Item['pid']",
"code": "UnexpectedParameter",
"time": "2015-11-26T15:51:33.934Z"
}
],
"time": "2015-11-26T15:51:33.944Z"
}
我不明白为什么或如何获取键 0、1、2、3、4 和 5,而它们在上一行打印时不存在。
此外,如何修复错误 Expected params.Item['pid'] to be a structure
?我已经将它声明为一个字符串,并且正在尝试存储一个字符串!
其他注意事项: 当我在 shell 上 运行 时,我在函数中使用的相同代码工作得很好。我还包含了 aws-sdk 并根据需要对其进行了配置:
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
AWS.config.endpoint = 'http://localhost:8000/'
var dynamodb = new AWS.DynamoDB();
注意:如以下多条评论所述,此答案可能不再有效。
从 nodejs 向数据库中添加记录必须使用的函数是 put
而不是 DynamoDB shell 中使用的 putItem
。将上面的函数更改为以下修复它。
function(request, response) {
params = {
TableName: 'TABLE-NAME',
Item: {
pid: 'abc123'
}
};
console.log(params);
dynamodb.put(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
}
putItem()
method on the AWS.DynamoDB
class 期望 params.Item
对象被格式化为 AttributeValue 表示。这意味着你必须改变这个:
params = {
TableName: 'TABLE-NAME',
Item: {
pid: 'abc123'
}
};
进入这个:
params = {
TableName: 'TABLE-NAME',
Item: {
pid: {
S: 'abc123'
}
}
};
如果您想使用本机 javascript 对象,您应该使用 AWS.DynamoDB.DocumentClient
class,它会自动将 Javascript 类型编组到 DynamoDB AttributeValues 上,如下所示:
- 字符串 -> S
- 数量 -> N
- 布尔值 -> 布尔值
- 空 -> 空
- 数组 -> L
- 对象 -> M
- Buffer、File、Blob、ArrayBuffer、DataView 和 JavaScript 类型数组 -> B
它提供了一个put()
method, that delegates to AWS.DynamoDB.putItem()
.