本文介绍了错误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));
});

此函数被调用以向DB中添加元素(在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:\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在上一行打印时不存在时。

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.

此外,如何解决错误期望params.Item ['pid']为结构?我已将其声明为字符串,并尝试存储字符串!

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!

其他说明:
我在该函数中使用的相同代码仅能正常工作我在外壳上运行它很好。我还包括了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();


推荐答案

方法类期望将 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对象,应该使用类,该类会自动将Javascript类型编组到DynamoDB AttributeValue上,如下所示:


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:


  • String-> S

  • 数字-> N

  • 布尔值-> BOOL

  • 空-> NULL

  • Array-> L

  • Object-> M

  • 缓冲区,文件,Blob,ArrayBuffer,DataView和JavaScript类型的数组-> B

  • String -> S
  • Number -> N
  • Boolean -> BOOL
  • null -> NULL
  • Array -> L
  • Object -> M
  • Buffer, File, Blob, ArrayBuffer, DataView, and JavaScript typed arrays -> B

它提供了方法,该方法委派给。

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

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

08-28 06:55