错误 InvalidParameterType:在 DynamoDB 中预期 params.Item['pid'] 是一个结构。

39
注意:所有这些都发生在 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));
});

这是在node.js中被调用以向DB添加元素的函数:

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();
2个回答

96

putItem() 方法在 AWS.DynamoDB 类中,期望 params.Item 对象的格式为 AttributeValue。这意味着你需要将以下内容进行更改:

params = {
  TableName: 'TABLE-NAME',
  Item: {
    pid: 'abc123'
  }
};

转换为:

params = {
  TableName: 'TABLE-NAME',
  Item: {
    pid: {
      S: 'abc123'
    }
  }
};
如果想使用本机JavaScript对象,应该使用AWS.DynamoDB.DocumentClient类,它会自动将Javascript类型编组到DynamoDB AttributeValues中,如下所示:
  • 字符串 -> S
  • 数字 -> N
  • 布尔值 -> BOOL
  • null -> NULL
  • 数组 -> L
  • 对象 -> M
  • 缓冲区、文件、Blob、ArrayBuffer、DataView和JavaScript类型数组 -> B
它提供一个put()方法,委派给AWS.DynamoDB.putItem()

0

注意:如下回答可能已经无效,根据下面的多个评论所述。

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

6
put 在 http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html 没有文档说明,当我尝试使用时,出现错误:"TypeError: dynamodb.put is not a function"。请帮忙检查一下。 - BlueMonkMN
5
put是一个函数,不属于DynamoDB本身,而是属于Dynamo的文档客户端(Document Client)。根据文档,文档客户端抽象化简化了使用AWS SDK for JavaScript读写Amazon DynamoDB的数据操作。在此处查看:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-document-client.html - Saras Arya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接