【发布时间】:2022-04-05 06:40:41
【问题描述】:
谁能解释一下在调用DocumentClient.get时如何使用GetItemInput类型?
如果我传入任何类型的对象 get 有效,但如果我尝试强烈键入 params 对象,则会收到此错误:
ValidationException: The provided key element does not match the schema
这是我的 lambda 函数代码,我将参数作为 any 类型传递:
export const get: Handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
console.log(event.pathParameters)
if (!event.pathParameters) {
throw Error("no path params")
}
const params: any = {
Key: {
id: event.pathParameters.id
},
TableName: table
}
console.log(params)
try {
const result: any = await dynamoDb.get(params).promise()
return {
body: JSON.stringify(result.Item),
statusCode: result.$response.httpResponse.statusCode
}
} catch (error) {
console.log(error)
return {
body: JSON.stringify({
message: `Failed to get project with id: ${event.pathParameters!.id}`
}),
statusCode: 500
}
}
}
这是我尝试让它与类型 GetItemInput 一起使用
export const get: Handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
console.log(event.pathParameters)
if (!event.pathParameters) {
throw Error("no path params")
}
const params: GetItemInput = {
Key: {
"id": { S: event.pathParameters.id }
},
TableName: table
}
console.log(params)
try {
const result: any = await dynamoDb.get(params).promise()
return {
body: JSON.stringify(result.Item),
statusCode: result.$response.httpResponse.statusCode
}
} catch (error) {
console.log(error)
return {
body: JSON.stringify({
message: `Failed to get project with id: ${event.pathParameters!.id}`
}),
statusCode: 500
}
}
}
如果我像以前一样离开Key ala:
const params: GetItemInput = {
Key: {
id: event.pathParameters.id
},
TableName: table
}
不出所料,我收到了类型错误。但无法理解我如何形成我的Key,这样我就得不到ValidationException。
注意id 字段在DynamoDB 中属于String 类型。
【问题讨论】:
-
您在哪里找到 GetItemInput?对我来说,它看起来不像 JS SDK 的一部分。我可以看到它是 Ruby 和 Go SDK 的一部分。
-
这是我的导入语句,它有效,所以假设它在 dynamo-db 类型中导入 { AttributeValue, GetItemInput, ScanInput, StringAttributeValue } from "aws-sdk/clients/dynamodb" docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/…
-
该链接表明这应该是有效的... const params: GetItemInput = { Key: { "id": event.pathParameters.id }, TableName: table },但是我得到一个类型错误说"type 'string' 与 type 'AttributeValue' 没有共同的属性"
-
我在链接中看不到任何引用?
-
SDK 专门抽象出属性值并改用本机 JSON 对象(请参阅您链接的页面顶部)。我可能是错的,但我不认为你正在尝试的是可能的。
标签: amazon-web-services aws-lambda amazon-dynamodb aws-sdk aws-sdk-js