简体   繁体   English

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

[英]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.我正在尝试将从网页中的字段创建的 json 文件发送到 AWS Lambda 中的节点 function 以将其添加到 DynamoDB 表中。 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.我制作了 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. 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 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();

    };

//}

Webpage js:网页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原始 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. 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);

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

相关问题 AWS lambda 函数 - 如何让 R 脚本从 node.js lambda 函数运行? - AWS lambda function -how to get R script to run from node.js lambda function? 如何从AWS Lambda node.js异步函数返回数据? - How to return data from AWS Lambda node.js asynchronous function? 如何从 Node.js Lambda 函数向 SNS 主题发送消息 - How to send SNS topic a message from Node.js Lambda function 如何在 AWS Lambda 节点 js 中发送 html 电子邮件,为 AWS api 网关返回格式正确的响应 - How to send a html email in AWS Lambda node js returning a well formed response for AWS api gateway 如何使用 Express 和 Node.js 从表单发送参数 - How to send parameters from a form using Express and Node.js How do I pass a JSON string to an AWS Lambda function from webpage Javascript without any API calls? - How do I pass a JSON string to an AWS Lambda function from webpage Javascript without any API calls? 将参数从玉发送到js / node - Send parameters from jade to js/node aws - 如何获取 lambda - 节点 - 使用查询参数作为事件属性 - aws - how to get lambda - node - to use query parameters as event attributes 不会调用AWS Lambda和Node.js函数 - AWS Lambda and Node.js function not getting called AWS Lambda / Node.js函数调用序列 - AWS Lambda/Node.js function call sequence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM