本文介绍了错误 InvalidParameterType:预期 params.Item['pid'] 是 DynamoDB 中的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note: all these are happening on the local instance of DynamoDB.

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

This is the code that I've used to create a table from the 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 中):

This is the function that is being called to add elements into the DB (in 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:
* InvalidParameterType: Expected params.Item['pid'] to be a structure
* UnexpectedParameter: Unexpected key '0' found in params.Item['pid']
* UnexpectedParameter: Unexpected key '1' found in params.Item['pid']
* UnexpectedParameter: Unexpected key '2' found in params.Item['pid']
* UnexpectedParameter: Unexpected key '3' found in params.Item['pid']
* UnexpectedParameter: Unexpected key '4' found in params.Item['pid']
* 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,而它们在上一行打印时不存在.

I don't understand why or how it is getting keys 0, 1, 2, 3, 4 and 5 when they aren't present on being printed in the previous line.

另外,我该如何修复错误Expected params.Item['pid'] to be a structure?我已将其声明为字符串并尝试存储字符串!

Also, how do I fix the error Expected params.Item['pid'] to be a structure? I have declared it as a string and am trying to store a string!

其他说明:当我在 shell 上运行它时,我在函数中使用的相同代码工作得很好.我还包含了 aws-sdk 并根据需要对其进行了配置:

Other notes:The same code that I've used in the function works just fine when I run it on the shell. I have also included the aws-sdk and have configured it as required:

var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
AWS.config.endpoint = 'http://localhost:8000/'
var dynamodb = new AWS.DynamoDB();

推荐答案

putItem() 方法="noreferrer">AWS.DynamoDB 类期望 params.Item 对象被格式化为 AttributeValue 表示.这意味着你必须改变它:

The putItem() method on the AWS.DynamoDB class is expecting the params.Item object to be formatted as a AttributeValue representation. That means you would have to change this:

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

进入这个:

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

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


If you want to use native javascript objects you should use the AWS.DynamoDB.DocumentClient class, that automatically marshals Javascript types onto DynamoDB AttributeValues like this:

  • 字符串 -> S
  • 数字 -> N
  • 布尔值 -> 布尔值
  • 空 -> 空
  • 数组 -> L
  • 对象 -> M
  • Buffer、File、Blob、ArrayBuffer、DataView 和 JavaScript 类型数组 -> B

它提供了一个 put() 方法,委托给 AWS.DynamoDB.putItem().

It provides a put() method, that delegates to AWS.DynamoDB.putItem().

这篇关于错误 InvalidParameterType:预期 params.Item['pid'] 是 DynamoDB 中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:55