Amazon dynamodb Error InvalidParameterType:参数项[';pid';]应为DynamoDB中的结构

Amazon dynamodb Error InvalidParameterType:参数项[';pid';]应为DynamoDB中的结构,amazon-dynamodb,dynamo-local,Amazon Dynamodb,Dynamo Local,注意:所有这些都发生在DynamoDB的本地实例上 这是我用来从DynamoDB Shell创建表的代码: var params = { TableName: "TABLE-NAME", KeySchema: [ { AttributeName: "pid", KeyType: "HASH" } ], AttributeDefinitions: [ { AttributeName: "pid",

注意:所有这些都发生在DynamoDB的本地实例上

这是我用来从DynamoDB Shell创建表的代码:

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));
});
这是为将元素添加到DB(在node.js中)而调用的函数:

我得到的结果是:

{ 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,而它们在前一行打印时不存在

另外,如何将错误
预期参数项['pid']修复为结构
?我已将其声明为字符串,并正在尝试存储字符串

其他说明: 当我在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));
  });
}
类上的方法要求将
params.Item
对象格式化为AttributeValue表示形式。这意味着您必须更改此选项:

params={
TableName:“TABLE-NAME”,
项目:{
pid:'abc123'
}
};
为此:

params={
TableName:“TABLE-NAME”,
项目:{
pid:{
S:‘abc123’
}
}
};

如果要使用本机javascript对象,应使用类,该类会自动将javascript类型封送到DynamoDB AttributeValue上,如下所示:

  • 字符串->S
  • 数字->N
  • 布尔->布尔
  • 空->空
  • 数组->L
  • 对象->M
  • 缓冲区、文件、Blob、ArrayBuffer、DataView和JavaScript类型的数组->B

它提供了一种方法,委托给。

put
未在处记录,当我尝试使用它时,我得到一个错误:“TypeError:dynamodb.put不是一个函数”.put不是Dynamo DB的函数,而是Dynamo的文档客户端的函数,通过文档,它是文档客户端抽象,使用AWS SDK for JavaScript可以更轻松地向Amazon DynamoDB读取和写入数据。过来看
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));
  });
}