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

编程入门 行业动态 更新时间:2024-10-10 11:29:35

AWS Lamda api无法从<a href=https://www.elefans.com/category/jswz/34/1770995.html style=浏览器运行,显示语法错误"/>

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无法从浏览器运行,显示语法错误

本文发布于:2024-05-07 07:57:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   语法错误   AWS   Lamda   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!