簡體   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