I need to check if a partition key exists (email) in a dynamodb table
I am using an astro action with typescript and the error it returns is:
ValidationException: The provided key element does not match the schema
I can see it in the console and if I search for it it will find it.
What am I doing wrong?
policies:
{
"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"
}
]
}
error:
ValidationException: The provided key element does not match the schema
at throwDefaultError (C:\visual_studio\votacion\node_modules.pnpm@smithy+smithy-client@3.4.2\node_modules@smithy\smithy-client\dist-cjs\index.js:836:20)
at C:\visual_studio\votacion\node_modules.pnpm@smithy+smithy-client@3.4.2\node_modules@smithy\smithy-client\dist-cjs\index.js:845:5
at de_CommandError (C:\visual_studio\votacion\node_modules.pnpm@aws-sdk+client-dynamodb@3.677.0\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@smithy+middleware-serde@3.0.8\node_modules@smithy\middleware-serde\dist-cjs\index.js:35:20
at async C:\visual_studio\votacionl\node_modules.pnpm@aws-sdk+lib-dynamodb@3.677.0_@aws-sdk+client-dynamodb@3.677.0\node_modules@aws-sdk\lib-dynamodb\dist-cjs\index.js:166:30
at async C:\visual_studio\votacion\node_modules.pnpm@smithy+core@2.5.1\node_modules@smithy\core\dist-cjs\index.js:168:18
at async C:\visual_studio\votacion\node_modules.pnpm@smithy+middleware-retry@3.0.25\node_modules@smithy\middleware-retry\dist-cjs\index.js:320:38
at async C:\visual_studio\votacion\node_modules.pnpm@aws-sdk+middleware-logger@3.667.0\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'
}
code:
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
}
}
You're using a GetItem command which expects a full primary key as parameter, however you only pass the partition key. To use just the partition key, simply change the command to 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
}
}