我正在从AWS Lambda对DynamoDB表执行查询。使用AWS.DynamoDB.DocumentClient时,它似乎无法正确转换“ N”数据类型。我收到错误“ ValidationException:一个或多个参数值无效:条件参数类型与架构类型不匹配”。当我使用AWS.DynamoDB并显式指定“ N”类型时,该查询有效。这两个“事件”值都是字符串。
我更喜欢使用AWS.DynamoDB.DocumentClient。有什么建议么?
->不起作用
let dynamodb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
let params2 = {
TableName: "TABLENAME",
KeyConditionExpression: "#o = :owner and #s = :sortkey",
ExpressionAttributeNames: {
"#o" : "owner",
"#s" : "sortkey
},
ExpressionAttributeValues: {
":owner" : event.userId,
":sortkey" : event.objPartitionKey
}
};
const data = await dynamodb.query(params2).promise();
->工作
let ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
// configure the dynamoDb query parameters
let params = {
TableName: "TABLENAME",
KeyConditionExpression: "#o = :owner and #s = :sortkey",
ExpressionAttributeNames: {
"#o" : "owner",
"#s" : "sortkey"
},
ExpressionAttributeValues: {
":owner" : {S: event.userId},
":sortkey" : {N: event.objPartitionKey}
}
};
const data = await ddb.query(params).promise();
答案 0 :(得分:0)
DynamoDB DocumentClient使用javascript类型来确定要编组的DynamoDB类型。我只能假设event.objPartitionKey是一个javascript字符串。它必须是一个JavaScript数字,才能正确编组到dynamoDB“ N”类型字段。要使第一个代码段起作用,请首先使用parseInt(event.objPartitionKey)
请参阅AWS文档以了解DocumentClient如何将JS类型编组为dynamodb类型: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html