我是 Lambda、SAM 和 DynamoDB 的新手。我想通過匹配email在template.yml. 當我呼叫該函式時,我得到的只是:
{"errorType":"ValidationException","errorMessage":"The provided key element does not match the schema"}
這是 SAM 模板定義
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 代碼。
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;
}
這有什么問題?
uj5u.com熱心網友回復:
TL;DR您的查詢缺少磁區鍵的值id。
您的表模式具有(“Hash”或“Partition”鍵)和(“Range”或“Sort”鍵) 的復合主鍵。執行DynamoDB 查詢操作,該操作根據主鍵值查找專案:idemaildocClient.get
您必須提供磁區鍵屬性的名稱和該屬性的單個值。查詢回傳具有該磁區鍵值的所有專案。或者,您可以提供排序鍵屬性并使用比較運算子來優化搜索結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/421541.html
標籤:
