node.js - 显示 ValidationException 的 AWS Lambda 更新函数
问题描述
我正在尝试通过 lamda 函数更新数据。我刚刚创建了一个 PUT 方法并在阅读文档后在方法中添加了 lambda 函数我创建了一个函数这里是代码
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});
AWS.config.update({region: 'us-east-2', apiVersion: '2012-08-10'});
var docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
const params = {
TableName: "Would-You-Rather",
Key:{
"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",
},
UpdateExpression: "set would = :w, rather = :r, wouldClick = :wC, , ratherClick = :rC ",
ExpressionAttributeValues:{
":w": "Working",
":r": "Working",
":wC": 12,
":rC": 1
},
ReturnValues:"UPDATED_NEW"
};
console.log("Updating the item...");
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
});
};
但是当我测试它显示的功能时
"message": "Invalid UpdateExpression: Syntax error; token: \",\", near: \", , ratherClick\"",
"code": "ValidationException",
如果我像这样包装 ExpressionAttributes
ExpressionAttributeValues:{
":w": "Working",
":r": "Working",
":wC": "12",
":rC": "1"
},
然后它显示这个错误
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Invalid or unexpected token",
这是我的 POST 方法,它工作正常添加这个,因为它可能有助于更好地理解
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) => {
const params = {
Item: {
"QuestionID": {
S: context.awsRequestId
},
"Would": {
S: event.would
},
"Rather": {
S: event.rather
},
"wouldClick": {
N: event.wouldClick
},
"ratherClick": {
N: event.ratherClick
}
},
TableName: "Would-You-Rather"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, data);
}
});
};
Codepen 代码
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather');
xhr.onreadystatechange = function(event) {
console.log(event.target.response);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');
xhr.send(JSON.stringify({Would: Coffe, Rather: Tea, wouldClick: 15, ratherClick: 13, QuestionID: 3e8976be-e763-4c69-84c3-be03ec4a38de}));
解决方案
有语法错误。你忘了把结束语放在这里
"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",
推荐阅读
- elasticsearch - Elasticsearch查询关键字值大于X的所有文档[7.2]
- ngrx - 如何覆盖/替换 NgRx 中的 DefaultDataService 并编写自定义 API 方法
- python - 我的标准神经网络成本正在上升
- javascript - “通过网络传输”大小大于资源大小
- ios - 错误:实例成员不能用于类型 viewController
- css - 嵌入 Vimeo 播放器(或任何 iframe 视频播放器)全宽和全高
- amazon-web-services - AWS + Springboot + 微服务 + Elastic BeanStalk + API Gateway + DynamoDB
- python - 修复多记录表中的错误日期
- python - 无法理解来自 Clickhouse 服务器的错误消息
- python - 如何让 if-loop 一直运行