无服务器和 GlobalSecondaryIndexes - 查询条件缺少 dynamodb 的关键模式元素

Serverless and GlobalSecondaryIndexes - Query condition missed key schema element with dynamodb

我尝试使用无服务器创建 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 前端中,它看起来像这样:

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

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

API Query

The Query operation finds items based on primary key values. You can query any table or secondary index that has a composite primary key (a partition key and a sort key).