By using AWS re:Post, you agree to the Terms of Use
/Lambda gets reference error saying event not defined?/

Lambda gets reference error saying event not defined?

0

I get a reference error saying event is not defined. So I'm trying to take event and get the attributes from it and save them into dynamo.

If i hard code the data, it inputs it into my database. so I know i'm getting the data input.

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();


exports.handler = async (event) => {

  try {
    await createItem()
    return { body: 'Successfully created item!' }
  } catch (err) {
    return { error: err }
  }
}

const params = {
  TableName: 'serverlessrepo-AestheticsFormDB-FormDataTable-1M0ZV9SXX6E4S',
      Item: {
      formId : event.formId,
    	CreditApp : event.creditApp,
    	FollowUp : event.followUp,
    	Injectables : event.injectables,
    	Marketing : event.marketing,
    	MeetAgain :"event.meetAgain,
    	MeetingRating :"event.scale,
    	NoOfPatients : event.patientCount,
    	ReachOut : event.reachOut,
    	SoloOwner : event.soloOwner
  }
}

async function createItem(){
  try {
    await docClient.put(params).promise();
  } catch (err) {
    return err;
  }

1 Answers
0
Accepted Answer

You initialize params outside the lambda handler context so the vent object is not know there.

Your code should look something like this:

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();


exports.handler = async (event) => {
  try {
    await createItem(event)
    return { body: 'Successfully created item!' }
  } catch (err) {
    return { error: err }
  }
}

async function createItem(event){
  params = {
    TableName: 'serverlessrepo-AestheticsFormDB-FormDataTable-1M0ZV9SXX6E4S',
    Item: {
          formId : event.formId,
    	  CreditApp : event.creditApp,
       	  FollowUp : event.followUp,
    	  Injectables : event.injectables,
    	  Marketing : event.marketing,
    	  MeetAgain : event.meetAgain,
    	  MeetingRating : event.scale,
    	  NoOfPatients : event.patientCount,
    	  ReachOut : event.reachOut,
    	  SoloOwner : event.soloOwner
     }
  }

  try {
    await docClient.put(params).promise();
  } catch (err) {
    return err;
  }
}
EXPERT
answered a month ago
  • genius! IT WORKED! a little different than what you shared but yes it worked!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions