0
votes

Part of the project, I want to write data entered into my web page from onto to a DynamoDB database, for this, I have written a Node.js code in AWS lambda to write items into a DynamoDB table. Created a web page form with more than one entries for users to fill required information and created an API Gateway to connect Lambda and HTML web page. Below are the codes for Lambda, API gateway, and HTML form. Please go through them.

Lambda My code:

exports.handler = function (e,ctx,callback) {
"use strict";

var params = {

 Item : {

            marchp : e.stepno,
            Prev_step_no :e.prevstepno,
            Next_step_no: e.nextstepno,
            Inputdata : e.inputdata,
            Acknowledgement: e.acknowledgement,
            Condition: e.condition,
            },
      TableName : 'MARCHPevents'
    };

API Gateway Body Mapping Templates:

 { 
    "stepno": $input.json("$.stepno"),
    "prevstepno": $input.json("$.prevstepno"),
    "nextstepno": $input.json("$.nextstepno"),
    "inputdata": $input.json("$.inputdata"),
    "acknowledgement": $input.json("$.acknowledgement"),
    "condition": $input.json("$.condition")
   }

HTML Code pasing data to API gateway:

url:API_URL,
success: function(data){
$('#entries').html('');

data.Items.forEach(function(MARCHPreventsItem){
$('#entries').append('<p>' + MARCHPreventsItem.InputData + '</p>');
})
}
});
});

$('#submitButton').on('click', function () {
    $.ajax({
        type: 'POST',
        url: API_URL,
        data: JSON.stringify({ "stepno": $('#s1').val() }),
        data: JSON.stringify({ "prevstepno": $('#p1').val() }),
        data: JSON.stringify({ "nextstepno": $('#n1').val() }),
        data: JSON.stringify({ "inputdata": $('#msg').val() }),
        data: JSON.stringify({ "acknowledgement": $('#ack').val() }),
        data: JSON.stringify({ "condition": $('#con').val() }),
        contentType: "application/json",
        success: function (data) {
            location.reload();
        }
    });
    return false;
});

If on passed one value from HTMLwebpage form to API gateway to passing to perfectly to Lambda which writing that one value to DynamoDB.

Problem Facing is passing more than one value from HTML web page form to API gateway this time it is having an invocation error at Lambda

Any Help?

1
Your code is incomplete and has syntax errors (see jQuery.ajax()). Please go through your code again and add all relevant info that could help identify your problem. For example, what DynamoDB / DocumentClient method does your Lambda function use? - Khalid T.

1 Answers

0
votes

Your JavaScript looks incorrect - you are overwriting the data parameter. You need to set the properties on the data object, i.e.

    var obj = {
        stepno : ${'#s1'}.val(),
        prevstepno : ${'#s2'}.val(),
        ...
    }

    $.ajax({
        type: 'POST',
        url: API_URL,
        data: obj,
        contentType: "application/json",
        success: function (data) {
            location.reload();
        }
    });