You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Astro+TypeScript查询DynamoDB分区键时遇ValidationException错误排查

问题

在使用Astro Action结合TypeScript开发时,尝试检查DynamoDB表中作为分区键的email是否存在,返回错误:ValidationException: 提供的键元素与架构不匹配。控制台中能看到该email,手动搜索也能找到对应条目,但代码调用报错。

表结构

分区键为email,类型为字符串。

IAM策略配置

{
  "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: 提供的键元素与架构不匹配
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'
}

业务代码

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);
    // 检查条目是否存在,返回voto值或false
    return result.Item ? result.Item.voto : false;
  } catch (error) {
    console.error("查询出错:", error);
    return false; // 出错时返回false
  }
}
解决方案

针对ValidationException: 提供的键元素与架构不匹配错误,按以下步骤排查修复:

  1. 核对分区键名称大小写:DynamoDB键名称区分大小写,检查表结构中分区键的实际名称(比如是否为Email而非email),确保代码中Key对象的键名与表定义完全一致。
  2. 校验数据类型一致性:确认表中email字段为字符串类型,同时检查传入的emailVoto是否为有效字符串。若存在类型异常,可在代码中强制转换:
    Key: {
      email: String(emailVoto).trim()
    }
    
  3. 检查DynamoDB客户端配置:确保@/aws中的dynamodb实例是通过DynamoDBDocumentClient正确包装的(@aws-sdk/lib-dynamodb要求使用增强客户端),示例初始化代码:
    import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
    import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
    
    const client = new DynamoDBClient({ region: "你的AWS区域" });
    export const dynamodb = DynamoDBDocumentClient.from(client);
    
  4. 确认表名拼写:检查代码中TableName的值votos与实际DynamoDB表名完全一致,包括大小写和特殊字符。

内容的提问来源于stack exchange,提问作者Figueduff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 20:55:54