使用Node.JS更新DynamoDB表返回保留关键字错误



我正试图按照本AWS教程更新DynamoDB表,但我收到了以下错误:

Unable to update item. Error JSON: {
"message": "Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: hash",
"code": "ValidationException",
"time": "2021-11-10T07:54:18.832Z",
"requestId": "1e657754-c33d-4a55-8245-a107765e4261",
"statusCode": 400,
"retryable": false,
"retryDelay": 43.13890004813747
}

我的代码如下:

import AWS from 'aws-sdk';
var table = "Users";
export async function updateData(email, hash, callback){ 
AWS.config.update({
region: 'us-west-2',
endpoint: 'http://localhost:8000'
});
var docClient = new AWS.DynamoDB.DocumentClient()
var params = {
TableName: table,
Key:{
"email": email
},
UpdateExpression: "set hash = :var",
ExpressionAttributeValues:{
":var": hash
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
callback(err, null);
} else {
callback(data, null);
}
});
}
updateData('joejava@icloud.com', 'hash', function(data, err){
console.log(err, data);
})

任何表格都有以下字段:

  • 电子邮件(密钥(
  • first_name
  • last_name
  • 散列

据我所见,我的代码与示例非常相似。我在那里错过了什么?

hash是一个dynamodb保留关键字,因此我们必须将其替换为ExpressionAttributeName

var params = {
TableName: table,
Key:{
"email": email
},
UpdateExpression: "set #k = :var",
ExpressionAttributeNames:{
"#k": "hash"
},
ExpressionAttributeValues:{
":var": hash
},
ReturnValues:"UPDATED_NEW"
};

相关内容

最新更新