最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

AWS Lamda api无法从浏览器运行,显示语法错误

IT培训 admin 14浏览 0评论

AWS Lamda api无法从浏览器运行,显示语法错误

我创建了一个简单的方法来更新可动态生成的项目。如果我在lambda函数中对值进行了硬编码,并从lamda中对其进行了测试,则其工作正常,但问题是当我尝试从事件中添加值时,它显示了一些语法错误。

这是我的lamda函数

const AWS = require('aws-sdk');
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": event.QuestionID,
    },
    UpdateExpression: "set Would = :w, Rather = :r, wouldClick = :wC, ratherClick = :rC",
    ExpressionAttributeValues:{
        ":w": event.Would,
        ":r": event.Rather,
        ":wC": event.wouldClick,
        ":rC": event.ratherClick
    },
    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));
    }
});

};

这是我正在lamda进行的测试活动

{
  "Would": "Soap",
  "Rather": "Oil",
  "wouldClick": "50",
  "ratherClick": "13",
  "QuestionID": "16563fa7-e833-445f-a76b-a9fbaab3a301"
}

而且它工作正常,但是当我尝试从Codepen运行时,这里显示的语法错误是我的代码

var xhr = new XMLHttpRequest();
xhr.open('PUT', '');
xhr.onreadystatechange = function(event) {
  console.log(event);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');

xhr.send(JSON.stringify({Would: Coffe,Rather: Tea, wouldClick: 15, ratherClick: 13, QuestionID: 16563fa7-e833-445f-a76b-a9fbaab3a301}));

显示此错误

level: "ERROR"
line: -6
message: "Uncaught SyntaxError: Unexpected token '{'"
type: "js"
回答如下:

您的QuestionID的GUID应该在其周围加上字符串引号,例如:

QuestionID: "16563fa7-e833-445f-a76b-a9fbaab3a301"

AWS Lamda api无法从浏览器运行,显示语法错误

我创建了一个简单的方法来更新可动态生成的项目。如果我在lambda函数中对值进行了硬编码,并从lamda中对其进行了测试,则其工作正常,但问题是当我尝试从事件中添加值时,它显示了一些语法错误。

这是我的lamda函数

const AWS = require('aws-sdk');
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": event.QuestionID,
    },
    UpdateExpression: "set Would = :w, Rather = :r, wouldClick = :wC, ratherClick = :rC",
    ExpressionAttributeValues:{
        ":w": event.Would,
        ":r": event.Rather,
        ":wC": event.wouldClick,
        ":rC": event.ratherClick
    },
    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));
    }
});

};

这是我正在lamda进行的测试活动

{
  "Would": "Soap",
  "Rather": "Oil",
  "wouldClick": "50",
  "ratherClick": "13",
  "QuestionID": "16563fa7-e833-445f-a76b-a9fbaab3a301"
}

而且它工作正常,但是当我尝试从Codepen运行时,这里显示的语法错误是我的代码

var xhr = new XMLHttpRequest();
xhr.open('PUT', '');
xhr.onreadystatechange = function(event) {
  console.log(event);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');

xhr.send(JSON.stringify({Would: Coffe,Rather: Tea, wouldClick: 15, ratherClick: 13, QuestionID: 16563fa7-e833-445f-a76b-a9fbaab3a301}));

显示此错误

level: "ERROR"
line: -6
message: "Uncaught SyntaxError: Unexpected token '{'"
type: "js"
回答如下:

您的QuestionID的GUID应该在其周围加上字符串引号,例如:

QuestionID: "16563fa7-e833-445f-a76b-a9fbaab3a301"
发布评论

评论列表 (0)

  1. 暂无评论