Error InvalidParameterType: Expected params.Item['pid'] to be a structure in DynamoDB

落花浮王杯 提交于 2019-12-03 23:01:46
birnbaum

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

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));
  });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!