我有下一个代码在几个lambdas中使用docClient访问DynamoDB:
params = {
TableName : tableName,
Key: {
publisherId: publisherId, // partition part
id: publicationId // sort part
}
};
docClient.query(params, (err, data) => {
//simplified for debugging purposes
callback({ err: err, params: params, data: data});
});
问题是这段代码在某些地方很有用,但在其他地方它会返回错误。在这种情况下,完整的回调结果是:
{
"err": {
"message": "Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.",
"code": "ValidationException",
"time": "2018-04-05T06:54:28.542Z",
"requestId": "ELEG6OB4L0RIJ5T4JL3MGUFJQNVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 4.5118545729119
},
"params": {
"TableName": "publications",
"Key": {
"publisherId": "8a47530c-c1d9-4dc4-95b5-809361ae27c2",
"id": "019b0d50-37e4-11e8-9972-9997bb75950e"
}
},
"data": null
}
可能是什么原因(如果我确定键值是正确的)? 当然,我可以在每个地方更改代码以使用“KeyConditionExpression”和“ExpressionAttributeValues”来解决问题。但问题不在于此。
答案 0 :(得分:2)
Query没有名为Key
的参数。您需要改为使用KeyConditionExpression
。
注意:我不确定为什么您使用query代替get项,因为您通过特定主键访问该表,而不是索引或排序键的表达式。
params = {
TableName: "tableName",
KeyConditionExpression: "#P = :p AND #ID = :id",
ExpressionAttributeNames: {
"#P": "publisherId",
"#ID": "id"
},
ExpressionAttributeValues: {
":p": publisherId, // Partition key
":id": publicationId // Sort key
}
};
docClient.query(params, (err, data) => {
//simplified for debugging purposes
callback(null, {err, data});
});