错误InvalidParameterType:期望params.Item ['pid']成为DynamoDB中的结构

roh*_*hpr 18 amazon-dynamodb dynamo-local

注意:所有这些都发生在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));
});
Run Code Online (Sandbox Code Playgroud)

这是被调用以将元素添加到DB中的函数(在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));
  });
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

{ 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"
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么或如何获得键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();
Run Code Online (Sandbox Code Playgroud)

bir*_*aum 53

该类的putItem()方法AWS.DynamoDB是期望将params.Item对象格式化为AttributeValue表示.这意味着你必须改变这个:

params = {
  TableName: 'TABLE-NAME',
  Item: {
    pid: 'abc123'
  }
};
Run Code Online (Sandbox Code Playgroud)

进入:

params = {
  TableName: 'TABLE-NAME',
  Item: {
    pid: {
      S: 'abc123'
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

如果你想使用本机javascript对象,你应该使用AWS.DynamoDB.DocumentClient该类,它自动将Javascript类型编组到DynamoDB AttributeValues上,如下所示:

  • 字符串 - > S.
  • 数字 - > N.
  • Boolean - > BOOL
  • null - > NULL
  • 数组 - > L.
  • 对象 - > M.
  • Buffer,File,Blob,ArrayBuffer,DataView和JavaScript类型数组 - > B.

它提供了put()一种委托的方法AWS.DynamoDB.putItem().