Skip to main content
Best answer

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

  • December 2, 2021
  • 5 replies
  • 204 views

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};

 

Best answer by Troy TessaloneBest answer by Troy Tessalone

@so-zap101 

Change this from “inputData” to “events”

 

Change this to: const text = inputData.events;

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Troy Tessalone
Forum|alt.badge.img+14

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?

 


  • Author
  • Beginner
  • 3 replies
  • December 3, 2021

Thank you!

I can, please see below:

 

 


Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 30948 replies
  • Answer
  • December 3, 2021

@so-zap101 

Change this from “inputData” to “events”

 

Change this to: const text = inputData.events;

 


  • Author
  • Beginner
  • 3 replies
  • December 3, 2021

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.


Troy Tessalone
Forum|alt.badge.img+14

@so-zap101 

See the warning under the Code input: