Javascript 如何使用dynamo db和node js基于二级索引进行查询

Javascript 如何使用dynamo db和node js基于二级索引进行查询,javascript,node.js,amazon-dynamodb,aws-sdk,Javascript,Node.js,Amazon Dynamodb,Aws Sdk,如何使用dynamo db和node js使用辅助索引进行查询 这是到目前为止我的代码,但它不起作用 getObjectByAlternateKey: function (keyName, keyObj, indexName, callback, consistentRead) { var params = { TableName: this.tableName, KeyConditionExpression: keyName + ' = :v_key',

如何使用dynamo db和node js使用辅助索引进行查询

这是到目前为止我的代码,但它不起作用

getObjectByAlternateKey: function (keyName, keyObj, indexName, callback, consistentRead) {
    var params = {
        TableName: this.tableName,
        KeyConditionExpression: keyName + ' = :v_key',
        ExpressionAttributeValues: converters.jsObjectToDynamoMap({ ':v_key': keyObj }),
        IndexName: indexName,
        ConsistentRead: !!consistentRead
    };
    this.dynamo.query(params, function (error, data) {
        console.dir(data);
        if (error) {
            return callback(error);
        }
        if (data.Items && data.Items.length > 0) {
            if (data.Items.length !== 1) {
                console.warn("Got more than one item returned for query!", { query: params, data: data });
            }
            return callback(null, converters.dynamoMapToJsObject(data.Items[0]));
        }

        return callback(null, null);
    });
},
下面是我用来创建表的cloudformation模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "TableName": {
      "Description": "Table name to use",
      "Type": "String",
      "Default": "test-user-unique-ids-prod-ue1"
    },
    "ReadCapacityUnits": {
      "Description": "Provisioned read throughput",
      "Type": "Number",
      "Default": "100",
      "MinValue": "1",
      "MaxValue": "10000",
      "ConstraintDescription": "must be between 1 and 10000"
    },
    "WriteCapacityUnits": {
      "Description": "Provisioned write throughput",
      "Type": "Number",
      "Default": "100",
      "MinValue": "1",
      "MaxValue": "10000",
      "ConstraintDescription": "must be between 1 and 10000"
    }
  },
  "Resources": {
    "sparkUserUniqueIds": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "TableName": {
          "Ref": "TableName"
        },
        "AttributeDefinitions": [
          {
            "AttributeName": "guid",
            "AttributeType": "S"
          },
          {
            "AttributeName": "unique_id",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "guid",
            "KeyType": "HASH"
          }
        ],
        "GlobalSecondaryIndexes": [
          {
            "IndexName": "test-user-by-unique-id",
            "KeySchema": [
              {
                "AttributeName": "unique_id",
                "KeyType": "HASH"
              }
            ],
            "Projection": {
              "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
              "ReadCapacityUnits": {
                "Ref": "ReadCapacityUnits"
              },
              "WriteCapacityUnits": {
                "Ref": "WriteCapacityUnits"
              }
            }
          }
        ],
        "ProvisionedThroughput": {
          "ReadCapacityUnits": {
            "Ref": "ReadCapacityUnits"
          },
          "WriteCapacityUnits": {
            "Ref": "WriteCapacityUnits"
          }
        }
      }
    }
  }
}

记录你的
params
值,它应该是这样的

{ TableName: 'test-user-unique-ids-prod-ue1',
  IndexName: 'test-user-by-unique-id',
  KeyConditions: 
   { unique_id: 
      { ComparisonOperator: 'EQ',
        AttributeValueList: [ { S: 'test' } ] } } }

你能给我们描述一下桌子吗?如果您的回拨中出现错误,您能提及吗?请参阅编辑-我添加了其他详细信息扫描您是否能帮助我?