首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:提供的键元素与架构不匹配。

错误:提供的键元素与架构不匹配。
EN

Stack Overflow用户
提问于 2022-01-25 13:00:34
回答 1查看 1.7K关注 0票数 0

我对Lambda,SAM和DynamoDB都很陌生。我希望通过匹配DynamoDB属性从template.yml表中选择一个记录,该属性定义为template.yml中表定义中的排序键。当我调用这个函数时,我得到的只是:

代码语言:javascript
运行
复制
{"errorType":"ValidationException","errorMessage":"The provided key element does not match the schema"}

下面是SAM模板定义

代码语言:javascript
运行
复制
  CustomersTable:
    Type: AWS::DynamoDB::Table
    Properties:
      ProvisionedThroughput:
        ReadCapacityUnits: 50
        WriteCapacityUnits: 100
      AttributeDefinitions:       
        -
          AttributeName: "id"
          AttributeType: "S"
        -
          AttributeName: "email"
          AttributeType: "S"
      KeySchema:
        -
          AttributeName: "id"
          KeyType: "HASH"
        -
          AttributeName: "email"
          KeyType: "RANGE"

这是NodeJS代码。

代码语言:javascript
运行
复制
const { v4: uuidv4 } = require('uuid'); 
const dynamodb = require('aws-sdk/clients/dynamodb');
const docClient = new dynamodb.DocumentClient(); 
const tableCustomers = process.env.TABLE_CUSTOMERS;
 
exports.handlerFunction = async (event) => {
 
    const body = JSON.parse(event.body); 
    const email = (body.hasOwnProperty('email'))  ? body.email : null;   

    let emailData = await docClient.get({
        TableName: tableCustomers,
        Key: { email: email },
        AttributesToGet: ['email']
    }).promise(); 
 
    const response = {
        statusCode: 200,
        body: JSON.stringify({ EMAILDATA: emailData }),
        headers: {
            "Access-Control-Allow-Headers": "Content-Type",
            "Access-Control-Allow-Origin": "*", // Allow from anywhere 
            "Access-Control-Allow-Methods": "POST" // Allow only GET request 
        },
    };

    // All log statements are written to CloudWatch
    console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
    return response;
}

这里面有什么不对的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-25 19:27:35

TL;DR您的查询缺少分区键id的值。

您的表模式具有复合主键 of id ("Hash“或"Partition”键)和email (“范围”或“排序”键)。docClient.get执行一个DynamoDB查询操作,它根据主键值查找项:

您必须提供分区键属性的名称和该属性的单个值。查询返回具有该分区键值的所有项。或者,您可以提供一个排序键属性,并使用比较运算符来细化搜索结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70849059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档