AWS DynamoDB Data Mapper:查询条件缺少关键架构元素

时间:2018-10-26 16:27:19

标签: amazon-web-services amazon-dynamodb cloud

我使用AWS DynamoDB Data Mapper作为管理数据库的高级API。我正在尝试查询全局二级索引,但无法超出此异常。

GSI基于帖子的预定结束日期,格式为YYYY-MM-DD

当我尝试像这样构建查询时:

function getQueryPaginator( dataMapper: DataMapper, startKey: { [ key: string ]: any } ): QueryPaginator<Load> {
  const today: Date = new Date();
  const partitionKey: string = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;

  const queryOptions: QueryOptions = {
    indexName: 'PostingExpiryIndex',
    limit: 25
  };

  return dataMapper.query(
    Load,
    {
      partitionKey: partitionKey
    },
    queryOptions
  ).pages();
}

并使用它:

const paginator: QueryPaginator<Load> = getQueryPaginator( dataMapper, lastEvaluatedKey );

    for await ( const page of paginator ) {
      try {
        const loadsBatch: Array<Load> = [];
        page.forEach( ( load: LoadInterface ) => {
          // do some logic...
          loadsBatch.push( Object.assign( new Load(), load ) );
        } );
        dataMapper.batchPut( loadsBatch );
      } catch ( e ) {
        log.error( `Error occurred while processing expired loads: ${e}` );
        await handleError( e );
      }
    }

我收到以下异常:

  

ValidationException:查询条件缺少键架构元素       在Request.extractError(/Users/myworkspace/node_modules/aws-sdk/lib/protocol/json.js:48:27)

但是我可以使用AWS Document客户端的等效API查询和获取值:

var params = {
    TableName: 'loads',
    IndexName: 'PostingExpiryIndex', // optional (if querying an index)
    KeyConditionExpression: 'postingEndDate = :value', // a string representing a constraint on the attribute
    ExpressionAttributeValues: { // a map of substitutions for all attribute values
      ':value': '2018-10-26'
    }
};
docClient.query(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

0 个答案:

没有答案