TypeError: Cannot read properties of undefined (reading 'response') onSubmit @ Code.gs:3

2,535 views
Skip to first unread message

TylerLawz

unread,
Sep 21, 2023, 10:05:28 PM9/21/23
to Google Apps Script Community
Hi! I have a google form to sheets script and it is giving me this error for the code below.. 

TypeError: Cannot read properties of undefined (reading 'response')
onSubmit @ Code.gs:3

using this code

function onSubmit(e) {
 var discordUrl = '';
 var formResponse = e.response;
 var itemResponses = formResponse.getItemResponses();
 var channelMsg;
  var discordname, aboutme, software, experience, portfolio, advertisement, availability, agreement;

 for (i in itemResponses){  

   //I prefer to list the variables up here so I'm sure about the number of questions
   var rank, name, badge;

   //Make a separate if-condition for each question you have, and make sure the case and
   //punctuation match what's on your form
   if (itemResponses[i].getItem().getTitle() == "Video Editor Application")
   {
     //And then set your respective variable with the response from the form
     name = itemResponses[i].getResponse();
   }

   //And then you can just copy/paste each if-condition for each question you have, editing the title and variable for your question
    if (itemResponses[i].getItem().getTitle() == "What is your Discord username? (Ex: BostonAlex)")
   {
     discordname = itemResponses[i].getResponse();
   }
       if (itemResponses[i].getItem().getTitle() == "Tell me about yourself so we can learn who you are as a person!")
   {
     aboutme = itemResponses[i].getResponse();
   }
       if (itemResponses[i].getItem().getTitle() == "What editing software do you use?")
   {
     software = itemResponses[i].getResponse();
   }
       if (itemResponses[i].getItem().getTitle() == "How long have you been editing content for?")
   {
     experience = itemResponses[i].getResponse();
   }
       if (itemResponses[i].getItem().getTitle() == "Do you have a portfolio that shows previous work? If so, what is the link to it?")
   {
     portfolio = itemResponses[i].getResponse();
   }
       if (itemResponses[i].getItem().getTitle() == "Where did you hear about this opportunity to become an editor for BostonAlex?")
   {
     advertisement = itemResponses[i].getResponse();
   }
    if (itemResponses[i].getItem().getTitle() == "How much time do you have to dedicate to making videos?")
   {
     availability = itemResponses[i].getResponse();
   }
    if (itemResponses[i].getItem().getTitle() == "You understand + agree this is a paid position within the BostonAlex team and may be working alongside BostonAlex with editing the footage to his preference.")
   {
     agreement = itemResponses[i].getResponse();
   }
   //Here is where I manually edit the way the form displays in the discord channel
   //You can edit this however you want
   //Don't forget to use + to add variables
   //Use "\n" to create a new line
 }

  channelMsg = {
    content: "",
    embeds: [{
      color: 13056,
      title: "**New Video Editor Application** ::movie_camera:",
      url: "",
      description: "New Video Editor Application.",
      fields: [{
          name: "**Submitted by:**",
          value:  discordname,
      },
      {
          name: "**About yourself:**",
          value:  aboutme,
      },
      {
          name: "**Editing Software:**",
          value:  software,
      },
      {
          name: "Experience:",
          value:  experience,
      },
      {
          name: "Portfolio:",
          value:  portfolio,
      },
      {
          name: "Referral Method:",
          value:  advertisement,
      },
      {
          name: "Availability:",
          value:  availability,
      },
      {
          name: "Agreement:",
          value:  agreement,
      }],
      timestamp: new Date(),
  footer: {    
    text: ""
  }
  }
  ]};

 //Payload that goes in
 var payload = JSON.stringify(channelMsg);

 //Setting up the parameters
 var params = {
   headers: {
     'Content-Type': 'application/json',
     'Accept': 'application/json'
   },
   method: "POST",
   payload: payload,
   muteHttpExceptions: true
 };

 //API Call
 var response = UrlFetchApp.fetch(discordUrl, params);

 //This is stuff that gets logged in your View > Logs, can you comment this out
 Logger.log(response.getContentText());
 Logger.log(payload);
 Logger.log(channelMsg);
}
Message has been deleted

Bikram Purkait

unread,
Sep 22, 2023, 5:54:34 AM9/22/23
to Google Apps Script Community
This means that at the 3rd line, the variable e is undefined or does not have a property called 'response'. This error occurs because the onSubmit function is likely not receiving the expected event object.

Solution: Just Save the Script and close the Script window. And now test your form.
Message has been deleted

Freelancer Techworld

unread,
Sep 22, 2023, 7:37:11 AM9/22/23
to Google Apps Script Community
Hi,

It looks like you ran the script directly from the Script Editor and got that error message.

Please create a trigger for that function (with event: on form submit). After the trigger is created, check your script by submitting a Google Forms form. 
Reply all
Reply to author
Forward
0 new messages