0

这是我的 dynamoose 表模式seller

const schema = new dynamoose.Schema({
    PK: {
        type: String,   //ni letak emel.toLowerCase() + #main/business/delivery/ehailing
        hashKey: true,
    },
    SK: {        
        type: String,  
        rangeKey: true,
        "index": {  //utk 'auto' display kedai bila user ada kat location tu
            "name": "SKIndex",
            "global": true,
            "rangeKey": "location"
        }
    },
    "location": String,
}, {
    "saveUnknown": true,
    "timestamps": true
});

正如您在上面看到的,我创建了一个 GSI,其SK命名为 hashkey,rangeKey 为SKIndexrangeKey location。所以我尝试执行下面的查询

var SKIndex_search = "some value"
var locality = "some value too"
var filter = new dynamoose.Condition().where("SKIndex").eq(SKIndex_search).filter("location").beginsWith(locality);
var getResult = await Seller.query(filter).exec()

但它总是会返回错误"InvalidParameter: Index can't be found for query."

=============== 运行此查询时Seller.query(SKIndex_search).using("SKIndex").filter("location").beginsWith(locality).exec()

它将显示错误消息ValidationException: Query condition missed key schema element

完整的错误日志:

aws:dynamodb:describeTable:response - {
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "PK",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SK",
                "AttributeType": "S"
            },
            {
                "AttributeName": "location",
                "AttributeType": "S"
            }
        ],
        "TableName": "earthlings_seller",
        "KeySchema": [
            {
                "AttributeName": "PK",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "SK",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2021-06-26T20:50:13.233Z",
        "ProvisionedThroughput": {
            "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
            "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 312,
        "ItemCount": 1,
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/earthlings_seller",
        "GlobalSecondaryIndexes": [
            {
                "IndexName": "SKIndex",
                "KeySchema": [
                    {
                        "AttributeName": "SK",
                        "KeyType": "HASH"
                    },
                    {
                        "AttributeName": "location",
                        "KeyType": "RANGE"
                    }
                ],
                "Projection": {
                    "ProjectionType": "ALL"
                },
                "IndexStatus": "ACTIVE",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": 1,
                    "WriteCapacityUnits": 1
                },
                "IndexSizeBytes": 312,
                "ItemCount": 1,
                "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/earthlings_seller/index/SKIndex"
            }
        ]
    }
}
aws:dynamodb:query:request - {
    "ExpressionAttributeNames": {
        "#qra": "location"
    },
    "ExpressionAttributeValues": {
        ":qrv": {
            "S": "nilai"
        }
    },
    "TableName": "earthlings_seller",
    "IndexName": "SKIndex",
    "KeyConditionExpression": "begins_with (#qra, :qrv)"
}
4

1 回答 1

1

如 Dynamoose文档中所述,where包含一个关键属性。此键表示属性名称 ( SK),而不是索引名称 ( SKIndex)。

将您的代码更改为以下内容应该可以工作。

new dynamoose.Condition().where("SK").eq(SKIndex_search).filter("location").beginsWith(locality);

您还可以使用该using功能手动设置特定索引以运行查询。然而,这是可选的。Dynamoose 将使用一个系统来查看您的索引并选择最匹配您的查询的一个。

于 2021-06-27T01:34:40.807 回答