Question

Raw Webhook - Run Javascript error: Cannot read property of undefined

  • 2 December 2021
  • 5 replies
  • 29 views

Userlevel 1

I believe I nearly have this script working. I have two zap events, the first is a Catch Raw Hook event that successfully captures raw JSON from a webhook, the second is a Run Javascript event. The script works as intended, but I believe I am not correctly defining inputData at the beginning of the script, hence the error. This may be something simple I am overlooking, any guidance is appreciated!

The JSON being output from the Catch Raw Hook Event is as follows:

[{"testJSON":{"testfield1":"test","testfield2":"test","testfield3":"test","testfield4":1,"testfield5":"0","testfield6":"0","testfield7":"test","zipCode":"90210","city":"testfield8","testfield9":"test","testfield10":"testfield10","testfield11":"test","testfield12":"test","testfield13":"test"},"events":"2021-10-04\nPage visit 09:50:00\n - URL: google.com/testing123\nPage visit 09:55:50\n - URL: google.com/testing456\nPage visit 09:52:03\n - URL: google.com/testing789\n\n2021-11-04\nPage visit 13:40:03\n - URL: google.com/blah\n\n"}]

The Javascript being run in the second zap event is as follows, with “inputData” as the name set for the incoming data from the Catch Raw Hook event in the Run Javascript event:

const text = inputData[0].events;
const p1 = /(\d{4}-\d{2}-\d{2})~(.*?)~~/g;
let vals = [...text.replaceAll("\n", "~").matchAll(p1)];

const p2 = /page visit\s+([^~]*)~\s+-\s+url:\s+([^~]*)/ig;
let buffer = [];

vals.forEach(v=>{
  let t = v[1];
  let parsed = [...v[2].matchAll(p2)];
  parsed.forEach(p=>{
    buffer.push({dt: t, time: p[1], url: p[2]});  
  });
});

output = {buffer};

 


5 replies

Userlevel 7
Badge +12

Hi @so-zap101 

Check out the available help articles: https://zapier.com/apps/code/help

Can you post screenshots with how your Code step is configured?

 

Userlevel 1

Thank you!

I can, please see below:

 

 

Userlevel 7
Badge +12

@so-zap101 

Change this from “inputData” to “events”

 

Change this to: const text = inputData.events;

 

Userlevel 1

This does resolve the original error, which is all I can ask for given the scope of the question (thank you!). Hitting a minor “not a function” snag on text.replaceAll, but I can take a look at that. This code block is a little odd in that it runs successfully in other environments but runs into issues in Zapier.

Userlevel 7
Badge +12

@so-zap101 

See the warning under the Code input:

 

Reply