我需要检查 dynamodb 表中是否存在分区键(电子邮件)
我正在使用带有 typescript 的 astro 操作,它返回的错误是:
ValidationException:提供的关键元素与架构不匹配
我可以在控制台中看到它,如果我搜索它,它就会找到它。
我做错了什么?
政策:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::xxxx:user/xxxx"
},
"Action": [
"dynamodb:GetItem",
"dynamodb:Query",
"dynamodb:PutItem",
"dynamodb:Scan"
],
"Resource": "arn:aws:dynamodb:xxx:xxxxx:table/votos"
}
]
}
错误:
ValidationException: The provided key element does not match the schema
at throwDefaultError (C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@smithy\smithy-client\dist-cjs\index.js:836:20)
at C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@smithy\smithy-client\dist-cjs\index.js:845:5
at de_CommandError (C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@aws-sdk\client-dynamodb\dist-cjs\index.js:2233:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@smithy\middleware-serde\dist-cjs\index.js:35:20
at async C:\visual_studio\votacionl\node_modules.pnpm@[email protected]_@[email protected]\node_modules@aws-sdk\lib-dynamodb\dist-cjs\index.js:166:30
at async C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@smithy\core\dist-cjs\index.js:168:18
at async C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@smithy\middleware-retry\dist-cjs\index.js:320:38
at async C:\visual_studio\votacion\node_modules.pnpm@[email protected]\node_modules@aws-sdk\middleware-logger\dist-cjs\index.js:34:22
at async chequeaVoto (C:/visual_studio/votacion/src/actions/index.ts:53:20) {
'$fault': 'client',
'$metadata': {
httpStatusCode: 400,
requestId: 'sfadfasfasfas',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
__type: 'com.amazon.coral.validate#ValidationException'
}
代码:
import { dynamodb } from "@/aws";
import { GetCommand, PutCommand } from "@aws-sdk/lib-dynamodb";
async function chequeaVoto(emailVoto: string): Promise<string | false> {
console.log("emailVoto llego:", emailVoto);
const params = {
TableName: "votos",
Key: {
email: emailVoto,
},
};
try {
const result = await dynamodb.send(new GetCommand(params));
console.log("Resultado de la consulta chequeo:", result.Item);
// Verifica si existe el item y devuelve el voto o false
return result.Item ? result.Item.voto : false;
} catch (error) {
console.error("Error en la consulta:", error);
return false; // Devuelve false en caso de error
}
}
您使用的 GetItem 命令需要完整的主键作为参数,但您只传递分区键。要仅使用分区键,只需将命令更改为 QueryCommand:
import { dynamodb } from "@/aws";
import { QueryCommand, PutCommand } from "@aws-sdk/lib-dynamodb";
async function chequeaVoto(emailVoto: string): Promise<string | false> {
console.log("emailVoto llego:", emailVoto);
const params = {
TableName: "votos",
KeyConditionExpression: "#a = :v",
ExpressionAttributeNames: {
"#a": "email"
},
ExpressionAttributeValues: {
":v": emailVoto
}
};
try {
const result = await dynamodb.send(new QueryCommand(params));
console.log("Resultado de la consulta chequeo:", result.Items);
// Verifica si existe el item y devuelve el voto o false
return result.Items ? result.Items[0].voto : false;
} catch (error) {
console.error("Error en la consulta:", error);
return false; // Devuelve false en caso de error
}
}