尝试将JSON字符串作为有效负载传递后,AWS Lambda中出现意外的令牌错误

时间:2019-11-12 01:36:35

标签: javascript node.js json amazon-web-services aws-lambda

我是AWS的新手,我正在尝试从网页调用Lambda函数(节点10.x),将网页字段中的JSON字符串传递给Lambda函数以添加到Dynamo表中。我要说的是API,因为这是我第一次使用AWS,而我在Lambda中要做的就是在Dynamo和网页之间发送JSON字符串。我从日志中得到的错误是:

  

{“ errorType”:“ SyntaxError”,“ errorMessage”:“ JSON在位置0处意外的令牌u”,“ stack”:[“ SyntaxError:JSON在位置0处意外的令牌u”,“在JSON.parse ()“,”在Runtime.exports.handler(/var/task/index.js:5:30)“,”在Runtime.handleOnce(/var/runtime/Runtime.js:66:25)“]} < / p>

这是我的网页javascript:

var lambda = new AWS.Lambda();



function makeJSON(){

var userID = "";

var name = document.forms["characterForm"]["characterName"].value;

//alert(name);

//alert(typeof name);

var race = document.forms["characterForm"]["race"].value;

var playerClass = document.forms["characterForm"]["class"].value;

var strength = document.forms["characterForm"]["strength"].value;

var dexterity = document.forms["characterForm"]["dexterity"].value;

var constitution = document.forms["characterForm"]["constitution"].value;

var intelligence = document.forms["characterForm"]["intelligence"].value;

var wisdom = document.forms["characterForm"]["wisdom"].value;

var charisma = document.forms["characterForm"]["charisma"].value;



//alert(name + race + playerClass + strength, dexterity, constitution, intelligence, wisdom, charisma);


characterSheetObj = {userID: userID, name: name, race: race, class: playerClass, strength: strength, dexterity: dexterity, constitution: constitution, intelligence: intelligence, wisdom: wisdom, charisma: charisma}

characterSheetJSON = JSON.stringify(characterSheetObj);


alert(characterSheetJSON);


var myParams = {

FunctionName : 'addCharacterSheet',

InvocationType : 'RequestResponse',

LogType : 'None',



//Payload : '{"userID": userID, "name": name, "race": race, "class": playerClass, "strength": strength, "dexterity": dexterity, "constitution": constitution, "intelligence": intelligence, "wisdom": wisdom, "charisma" : charisma}'

Payload : characterSheetJSON

}


lambda.invoke(myParams, function(err, data){

//if it errors, prompts an error message

if (err) {

alert("Error");

prompt(err);

}

//otherwise puts up a message that it didnt error. the lambda function presently doesnt do anything

//in the future the lambda function should produce a json file for the JavaScript here to do something with

else {

alert("Invoked Lambda function without erroring!");

}

});


}

这是我在lambda函数中的Node 10.x代码


const AWS = require('aws-sdk');

const db = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = async (event) => {

// exports.handler = function(e, ctx, callback) {

var characterData = JSON.parse(event.body);

console.log(characterData);

alert(characterData);

let scanningParameters = {

TableName : 'characterTable',

Limit:100

};


db.scan(scanningParameters, function(err, data){

if(err){

callback(err,null);

}else{

callback(null,data);

}

});


const params = {

TableName : 'characterTable',

Item: {

name : 'Alan'

}


};


const userID = '12345';

params.Item.userID = userID;


return await db.put(characterData).promise();

};

我的JSON格式在某处可能是错误的,但是我不确定在哪里。任何指导将不胜感激!

2 个答案:

答案 0 :(得分:0)

您的JSON在这里无效

__getattribute__

用引号Kclass

括住键

答案 1 :(得分:0)

意外的""0x65中的u。这意味着您需要仔细阅读文档,并弄清楚为什么u未定义。