我有一个名为AllowListedItems 的DynamoDB 表。它具有 ID、名称和ignoreNoTraffic 字段。它在“name”字段上有一个名为 name-index 的 GSI。我正在尝试查询带有名称的ignoreNoTraffic 值。下面是我的 Typescript 代码。我使用 AWS SDK V3 进行以下导入:
import { DynamoDB, GetItemCommand, ScanCommand, QueryCommand } from "@aws-sdk/client-dynamodb";
import { unmarshall } from "@aws-sdk/util-dynamodb";
private async getIgnoreNoTraffic(): Promise<boolean> {
const params = {
TableName: Tables.ALLOW_LISTED_ITEMS,
IndexName: "name-index",
KeyConditionExpression: "#nameAttr = :nameValue",
ExpressionAttributeNames: {
"#nameAttr": "name"
},
ExpressionAttributeValues: {
":nameValue": this.name,
},
};
try {
const items = await dynamodbUtils.queryValuesFromDB(params);
if (items.length > 0) {
const item = items[0];
console.log("Received item:", item);
if (item. ignoreNoTraffic !== undefined) {
return item. ignoreNoTraffic.BOOL || false;
} else {
return false;
}
} else {
console.log("Item could not be fetched from the table using name");
return false;
}
} catch (error) {
console.error("Error fetching ignoreNoTraffic by name:", error);
throw error;
}
}
以下是dynamoDB功能:
export async function queryValuesFromDB(params: any) {
console.log("Querying DynamoDB with params2:", JSON.stringify(params, null, 2));
try {
const data = await client.send(new QueryCommand(params));
if (!data) {
console.error("DynamoDB returned undefined or null data.");
throw new Error("DynamoDB returned undefined or null data.");
}
console.log("Received raw data from DynamoDB:", JSON.stringify(data, null, 2));
if (!data.Items) {
console.log("Items field is missing from the data.");
throw new Error("DynamoDB response does not contain 'Items'.");
}
if (data.Items.length > 0) {
console.log("Successfully retrieved items:", data.Items);
return data.Items; // Directly return the items as JSON
} else {
console.log("No items found for the query.");
return [];
}
} catch (error) {
console.error("Error executing DynamoDB query:", error);
if (error instanceof Error) {
console.error("Error message:", error.message);
throw new Error(`DynamoDB Query Error: ${error.message}`);
} else {
console.error("Unknown error occurred:", JSON.stringify(error));
throw new Error("An unknown error occurred during DynamoDB query.");
}
}
}
当我运行上面的代码时,出现以下错误:
ERROR Error while sending query command to DynamoDB: TypeError: Cannot read properties of undefined (reading '0')
我有以下信息日志:
INFO Querying DynamoDB with params3:
{
"TableName": "AllowListedItems",
"IndexName": "name-index",
"KeyConditionExpression": "#nameAttr = :nameValue",
"ExpressionAttributeNames": {
"#nameAttr": "name"
},
"ExpressionAttributeValues": {
":nameValue": "development"
}
}
为什么我会收到此错误?感谢任何建议/帮助。
我尝试在 AWS 控制台上使用 AWS SDK V2 通过 javascript lambda 运行上述 params 命令。但这确实有效!
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();
const docClient = new AWS.DynamoDB.DocumentClient();
async function queryValuesFromDB(params) {
console.log("Querying DynamoDB with params:", JSON.stringify(params, null, 2));
try {
const data = await docClient.query(params).promise();
console.log("Received raw data from DynamoDB:", JSON.stringify(data, null, 2));
if (data && data.Items && data.Items.length > 0) {
console.log("Successfully retrieved items:", data.Items);
return data.Items;
} else {
console.log("No items found for the query.");
return [];
}
} catch (error) {
console.error("Error executing DynamoDB query:", error.message || error);
throw new Error(`DynamoDB Query Error: ${error.message || error}`);
}
}
async function getIgnoreNoTraffic(name) {
const params = {
TableName: 'AllowListedItems',
IndexName: 'name-index',
KeyConditionExpression: "#nameAttr = :nameValue",
ExpressionAttributeNames: {
"#nameAttr": "name"
},
ExpressionAttributeValues: {
":nameValue": name,
},
};
try {
const items = await queryValuesFromDB(params);
if (items.length > 0) {
const item = items[0];
if (item. ignoreNoTraffic !== undefined) {
return item. ignoreNoTraffic;
} else {
return false;
}
} else {
console.log("Item could not be fetched from the table using name");
return false;
}
} catch (error) {
console.error("Error fetching ignoreNoTraffic by name:", error);
throw error;
}
}
exports.handler = async (event) => {
const name = event.name || "test-name";
try {
const ignoreNoTraffic = await getIgnoreNoTraffic(name);
console.log("Ignore result:", ignoreNoTraffic);
return {
statusCode: 200,
body: JSON.stringify({
message: "Success",
ignoreNoTraffic: ignoreNoTraffic
}),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({
message: "Error",
error: error.message,
}),
};
}
};
在 V3 中,您不使用 docClient,但继续使用本机 JSON。
阅读这篇博客文章,它可以帮助您为客户端配置正确的参数:
https://aws.amazon.com/blogs/database/exploring-amazon-dynamodb-sdk-clients/