简体   繁体   中英

How to send parameters from js in a webpage to a node function in aws lambda?

I am attempting to send a json file created from fields in a webpage to a node function in AWS Lambda to add it to a DynamoDB table. I have the JSON made but I don't know how to pass it from the js used for the page to the lambda function. As this is for a class project, my group and I have decided to forego amazon's gateway API, and are just raw calling lambda functions using amazon's js sdk. I've checked Amazon's documentation and other various examples, but I haven't been able to find a complete solution.

Node function in lambda

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

exports.handler = async (event) => {

const params = {
    TableName : 'characterTable',
    Item: {
      name : 'defaultName'
    }

   };

    const userID = 'placeholder';
    params.Item.userID = userID;
    return await db.put(params).promise();

    };

//}

Webpage js:


var lambda = new AWS.Lambda();


function makeJSON(){
  var userID = "";
  var name = document.forms["characterForm"]["characterName"].value;

  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;


  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 : characterSheetJSON
  }

  lambda.invoke(myParams, function(err, data){
    //if it errors, prompts an error message
    if (err) {
            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("Did not error");
         }
  });



}


The html page for the raw javascript includes the proper setup for importing the sdk and configuring the region/user pool

I just don't know how to get the payload from the invocation in my node function, as this is my first time working with lambda and amazon's sdk, or doing any web development work at all, to be honest.

i would do it with async await. It's better to read.

lambda.invoke = util.promisify(lambda.invoke);
const result = await lambda.invoke(yourParams);
const payload = JSON.parse(result.Payload);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM