Error InvalidParameterType: Expected params.Item[&

2020-07-01 02:26发布

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

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

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

The output that I get is:

{ 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"
}

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.

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!

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();

2条回答
戒情不戒烟
2楼-- · 2020-07-01 03:20

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'
  }
};

Into this:

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

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
  • 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().

查看更多
Evening l夕情丶
3楼-- · 2020-07-01 03:26

Note: This answer may no longer be valid as mentioned in multiple comments below.

The function that must be used to add records into the database from nodejs is put and not putItem which is used in the DynamoDB shell. Changing the above function to the following fixed it.

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));
  });
}
查看更多
登录 后发表回答