Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js AWS Lambda更新函数显示ValidationException_Node.js_Amazon Web Services_Aws Lambda_Amazon Dynamodb - Fatal编程技术网

Node.js AWS Lambda更新函数显示ValidationException

Node.js AWS Lambda更新函数显示ValidationException,node.js,amazon-web-services,aws-lambda,amazon-dynamodb,Node.js,Amazon Web Services,Aws Lambda,Amazon Dynamodb,我正在尝试通过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-0

我正在尝试通过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",
如果我像这样包装ExpressionAttribute

ExpressionAttributeValues:{
    ":w": "Working",
    ":r": "Working",
    ":wC": "12",
    ":rC": "1"
},
然后它显示了这个错误

  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Invalid or unexpected token",
这是我的贴子方法,效果很好。加上这个,也许会有助于更好的理解

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);
        }
    });
};
代码笔代码

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",

是的,所以它抱怨Dynamo查询中的语法错误。我想这里可能是双逗号:
:wC,ratherClick
当我试图在codepen上测试它时,你能告诉我最后一件事吗?它显示了相同的语法错误?我在最后的代码笔问题中添加了代码