繁体   English   中英

如何将网页中js的参数发送到aws lambda中的节点function?

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

我正在尝试将从网页中的字段创建的 json 文件发送到 AWS Lambda 中的节点 function 以将其添加到 DynamoDB 表中。 我制作了 JSON,但我不知道如何将其从用于页面的 js 传递到 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. 我检查了亚马逊的文档和其他各种示例,但我无法找到完整的解决方案。

lambda 中的节点 function

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

    };

//}

网页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");
         }
  });



}


原始 javascript 的 html 页面包括导入 sdk 和配置区域/用户池的正确设置

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.

我会用异步等待来做。 最好阅读。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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