Search code examples
javascriptarrayszapier

Having trouble with Array.find in Zapier - Can't Read Property of Undefined


I'm trying to parse an array coming from a webhook response and provide the field "id" for use in another step of the Zap.

I'm using Array.find to parse the array and find the id.

Here's my code:

const result = JSON.parse(inputData.body);
return {
  result,
  SectionId: result.data.find(
    x => x.name === inputData.sectionText).id
};

and here's my webhook data

{"data":
[{"id":1124815237432228,"gid":"1124815237432228","name":"Last Name, First Name","resource_type":"task"},
{"id":1124814957166815,"gid":"1124814957166815","name":"Final Signing:","resource_type":"task"},
{"id":1124815361088518,"gid":"1124815361088518","name":"Matter Closing:","resource_type":"task"},
{"id":1124815216609386,"gid":"1124815216609386","name":"Setup:","resource_type":"task"},
{"id":1124815361502365,"gid":"1124815361502365","name":"Drafting:","resource_type":"task"}]}

This should pull out the id based on the value of sectionText. The variable sectionText comes from another Code by Zapier step in the Zap. If the sectionText were Drafting, for example, I would expect this code to pull the id, "1124815361502365". Instead, I receive the error "Cannot read property 'id' of undefined". Could it be related to the length of the array?

Any help is sincerely appreciated.


Solution

  • Verify what exact value is passed in inputData.sectionText property. Values in name contains ":", so if sectionText does not, then those two values never match.