首页 > 解决方案 > 显示 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}));

标签: node.jsamazon-web-servicesaws-lambdaamazon-dynamodb

解决方案


有语法错误。你忘了把结束语放在这里

"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",

推荐阅读