Submitted December 26, 2019 at 03:51PM by madmoneymcgee
Thursday, 26 December 2019
Node and AWS Lambda Help
I'm trying to use AWS Lambda and Node to write items to a DynamoDB table. I can hard-code the values I want to see with no problem but I can't quite figure out how to see anything when I'm writing to my front-end which is in React. But I get a 200 success message when looking at the network.I broadly adapted from this tutorial.Here's my function in Lambda:const AWS = require('aws-sdk'); const docClient = new AWS.DynamoDB.DocumentClient({region: "us-east-1"}); exports.handler = (event, context, callback) => { console.log("Processing..."); const params = { Item: { Corpus_Name: [], Source_Name: [] }, TableName: "corpusTest" }; const response = { statusCode: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true, }, body: JSON.stringify('Item Added'), }; docClient.put(params, function(err, data) { if(err){ callback(err, null); } else { callback(null, data); } }) }; With the const Params I can hard code whatever I want but I can't figure out what to put to tell it to actually take in what I type in my web-form.Here's my form.js in React:import React, { Component } from 'react'; import axios from 'axios'; export default class Form extends Component { constructor(props) { super(props); this.state = { Corpus_Name: '', Source_Name: '', }; this.handleSubmit = this.handleSubmit.bind(this); this.handleCorpusChange = this.handleCorpusChange.bind(this); this.handleSourceChange = this.handleSourceChange.bind(this); } handleCorpusChange = (event) => { this.setState({ Corpus_Name: event.target.value }); } handleSourceChange = (event) => { this.setState({ Source_Name: event.target.value }); } async handleSubmit(event) { event.preventDefault(); const { Corpus_Name, Source_Name } = this.state; await axios.post( 'https://15ix4rukfb.execute-api.us-east-1.amazonaws.com/default/serverlessAppFunction', { key1: `${Corpus_Name}, key2: ${Source_Name}` } ); } render() { return (
); } } Corpus_Name is my partition Key and Source_Name is my sort key if that helps.
Submitted December 26, 2019 at 03:51PM by madmoneymcgee
Submitted December 26, 2019 at 03:51PM by madmoneymcgee
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment