为什么getItem()返回"无效的属性值类型"?

时间:2017-08-26 16:00:50

标签: node.js amazon-dynamodb

我正在尝试从DynamoDB AWS获取单个项目,并且无法弄清楚如何使用散列和范围键来指定它。以下是describeTable和扫描输出:

// describeTable output:
{
  "Table": {
    "AttributeDefinitions": [
      {
        "AttributeName": "timepost",
        "AttributeType": "N"
      },
      {
        "AttributeName": "addr",
        "AttributeType": "S"
      }
    ],
    "TableName": "Scratch",
    "KeySchema": [
      {
        "AttributeName": "timepost",
        "KeyType": "HASH"
      },
      {
        "AttributeName": "addr",
        "KeyType": "RANGE"
      }
    ],
    "TableStatus": "ACTIVE",
    "CreationDateTime": "2017-08-24T07:08:19.650Z",
    "ProvisionedThroughput": {
      "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
      "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
      "NumberOfDecreasesToday": 0,
      "ReadCapacityUnits": 5,
      "WriteCapacityUnits": 5
    },
    "TableSizeBytes": 32,
    "ItemCount": 1,
    "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/Scratch"
  }
}

// scan output:
{
  "Items": [
    {
      "addr": "1.1.1.1:443",
      "ms": 67,
      "timepost": 12321340
    }
  ],
  "Count": 1,
  "ScannedCount": 1
}

这是我的getItem()代码和错误输出:

  docClient.get({
    TableName: "Scratch",
    Key: {
      timepost: {N: '12321340'},
      addr: {S: '1.1.1.1:443'}
  }}, opComplete);

err: {
  "message": "Invalid attribute value type",
  "code": "ValidationException",
  "time": "2017-08-26T15:56:59.938Z",
  "requestId": "f9d94f20-f22d-4141-be06-2eaba1eee5a1",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 26.507308215655236
}

我做错了什么?

3 个答案:

答案 0 :(得分:3)

想出来:

  docClient.get({
    TableName: "Scratch",
    Key: {
      timepost: 12321340,
      addr: '1.1.1.1:443'
  }}, opComplete);

这对我有用。我正在使用适用于Javascript的AWS开发套件2.102.0,以防万一。

答案 1 :(得分:0)

@RichAmberale,尝试指定与关键模式定义相同的类型,即

  docClient.get({
    TableName: "Scratch",
    Key: {
      timepost: {HASH: '12321340'},
      addr: {RANGE: '1.1.1.1:443'}
  }}, opComplete);

答案 2 :(得分:0)

您应该使用 DocumentClient.GetItemInput 而不是 DynamoDB.GetItemInput。我也有同样的痛苦