繁体   English   中英

使用 AWS SDK V3 和 Typescript 查询具有多列的 Dynamo DB

[英]Query Dynamo DB with multiple columns using AWS SDK V3 and Typescript

我有下面的一段代码,我尝试使用它来使用 3 列的值获取一行数据。

const client = new DynamoDB({region: "us-east-1"});
const params = {
TableName: Tables.TYPES,
ExpressionAttributeNames: {
    '#type': "type",
    '#region': "region",
    '#dock': "dock"
},
ExpressionAttributeValues: {
    ":type": type,
    ":dock": dock,
    ":region": region
 },
 KeyConditionExpression: '#type = :type AND #dock = :dock AND #region = :region'
 }
 let types: Record<string, NativeAttributeValue> = data = await client.send(new QueryCommand(params));
 console.log("Received data ", data);

但是,当我运行上面的代码时,出现以下错误。

"errorMessage": "Cannot read properties of undefined (reading '0')"

我在这里做错了什么? 如何使用 Dynamo DB 表中多列的值读取行? 顺便说一句,我正在使用@aws-sdk/client-dynamodb 感谢任何帮助。

更新:仍然无法正常工作:

const params = {
            TableName: Tables. TYPES,
            FilterExpression: 'type = :type AND dock = :dock AND region = :region',
            ExpressionAttributeValues: {
                ":type": {S: type},
                ":dock": {S: dock},
                ":region": {s: region}
            }
        }

KeyConditionExpression仅适用于键,您的分区键和排序键。 除此之外,如果您希望过滤非关键属性,则必须使用FilterExpression

假设type是您的分区键,参数将如下所示(进一步编辑以满足您的特定需求):

const params = {
TableName: Tables.TYPES,
ExpressionAttributeNames: {
    '#type': "type",
    '#region': "region",
    '#dock': "dock"
},
ExpressionAttributeValues: {
    ":type": type,
    ":dock": dock,
    ":region": region
 },
 KeyConditionExpression: "#type = :type",
 FilterExpression: "#dock = :dock AND #region = :region"
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM