1

I'm brand new to AWS, and I'm trying to invoke a Lambda function (Node 10.x) from a webpage, passing a JSON string from fields in the webpage to the Lambda function to add to a Dynamo table. I'm foregoing the API as this is my first time using AWS, and all I'll be doing in Lambda is sending JSON strings between Dynamo and the webpage. The error I'm getting from the logs is:

{ "errorType": "SyntaxError", "errorMessage": "Unexpected token u in JSON at position 0", "stack": [ "SyntaxError: Unexpected token u in JSON at position 0", " at JSON.parse ()", " at Runtime.exports.handler (/var/task/index.js:5:30)", " at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)" ] }

Here is my webpage 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!");

}

});


}

And here is my Node 10.x code in the lambda function


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();

};

My JSON formatting is probably wrong somewhere, but I'm not sure where. Any guidance would be greatly appreciated!

2 Answers 2

1

Your JSON is not valid here

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

Enclosed the keys with quotes ""

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the heads up, I fixed that as well. However, I believe the issue is also that I'm trying to avoid using Gateway when I probably should just use it
1

The unexpected u is the u from undefined. It means you need to go through docs and figure out why your event.body is undefined.

2 Comments

Some quick googling (namely this) suggests that I should be using a Gateway API for the HTTP requests, does this mean it's time for me to get used to Gateway, as I can't just forego it?
@nunzio this is unrelated to using or not using API Gateway. API Gateway invokes Lambda functions using exactly the same interface that the Javascript SDK uses. Please edit your question to include exactly the code you are using with any fixes you have made since you posted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.