Reputation: 51
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!
Upvotes: 1
Views: 15272
Reputation: 497
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.
Upvotes: 1
Reputation: 68715
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 ""
Upvotes: 1