0

We have this custom script for our hubspot contact form that basically redirects the contact to an other page and saving the filled contact informations as a query string in the url.

The script was working fine until today but it seems to not work anymore...

Here is the script :

<!-- this is the standard form embed code --> 
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
  hbspt.forms.create({
    region: "na1",
    portalId: "6914456",
    formId: "a29bb11e-d9d1-4ffd-a8f7-9e8038cad4c9"
  });
  // add custom script after it
  var array = [];
  window.addEventListener('message', event => {
    // in the onFormSubmit global form event, grab the data and push it to the above array
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
      let vars = ['irstname', 'lastname', 'email'];
      let len = vars.length;
      for (i = 0; i < len; i++) {
        for (j in event.data.data) {
          if (event.data.data[j]["name"] == vars[i]) {
            let object = {
              name: '',
              value: ''
            };
            object.name = vars[i];
            object.value = event.data.data[j]["value"];
            array.push(object);
          }
        }
      }
    }
    // after the form has submitted and data sent to Hubspot, get the data from the array and then redirect the page, adding the data to the URL
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
      var firstname = array[0].value
      var lastname = array[1].value
      var email = array[2].value
      window.location = "https://cleverconnect.com?firstName=" + encodeURIComponent( firstname ) +"&lastName=" + encodeURIComponent( lastname ) +"&email=" + encodeURIComponent( email );
    }
  });
</script>

And we get this error in the console

(index):278 Uncaught TypeError: Cannot read properties of undefined (reading 'value')
    at (index):278:32

Does someone know why it doesn't work, although we didn't change anything.

Thanks a lot !

  • Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). Try using your browser’s [debug capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). The dev tools provide a **Network** tab. If something in your code depends on a network request, is the resource _found_ (e.g. HTTP 200 response)? What is its response? – Sebastian Simon Oct 27 '22 at 15:27

0 Answers0