Serverless和GlobalSecondaryIndexes-使用dynamodb的查询条件缺少键架构元素

时间:2019-05-13 12:05:19

标签: node.js amazon-web-services amazon-dynamodb serverless aws-serverless

我尝试使用无服务器创建GlobalSecondaryIndexes,并在稍后查询。我得到

Query condition missed key schema element: id

这样做会出错,这很有意义,但是我的计划是查询特定日期的所有元素。

  var params = {
    TableName: process.env.DYNAMODB_TABLE_LIGHTHOUSE,
    KeyConditionExpression: 'fetchDate = :fetchDate',
    ExpressionAttributeValues: {
      ':fetchDate': { S: '2019-05-13' }
    }
  };

  const dbResult = await dynamoDb.query(params).promise()
  console.log(dbResult)

这是dynamodb的无服务器部分。可能这里缺少什么?

DynamoDbTableExpenses:
  Type: 'AWS::DynamoDB::Table'
  Properties:
    AttributeDefinitions:
      -
        AttributeName: id
        AttributeType: S
      -
        AttributeName: fetchDate
        AttributeType: S
    KeySchema:
      -
        AttributeName: id
        KeyType: HASH
    GlobalSecondaryIndexes:
      - IndexName: fetchDateIndex
        KeySchema:
          - AttributeName: fetchDate
            KeyType: HASH
        Projection:
          ProjectionType: ALL
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
    ProvisionedThroughput:
      ReadCapacityUnits: 1
      WriteCapacityUnits: 1
    TableName: ${self:provider.environment.DYNAMODB_TABLE_LIGHTHOUSE}

在Web前端中,它看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:2)

您需要添加IndexName属性。试试:

var params = {
    TableName: process.env.DYNAMODB_TABLE_LIGHTHOUSE,
    IndexName: 'fetchDateIndex',
    KeyConditionExpression: 'fetchDate = :fetchDate',
    ExpressionAttributeValues: {
      ':fetchDate': '2019-05-13'
    }
};

请参见API Query

  

查询操作基于主键值查找项目。您可以   查询任何具有复合主键的表或二级索引   分区键和排序键)。