Array.find not working in Zapier - Undefined property error

I’m stuck with a Zapier workflow. I’m trying to get an ‘id’ from an array in a webhook response. Here’s what I’m doing:

let parsed = JSON.parse(webhookData.body);
return {
  parsed,
  TaskId: parsed.items.find(
    item => item.title === inputData.taskName).id
};

My webhook data looks like this:

{"items":
[{"id":9876543210987654,"uid":"9876543210987654","title":"Smith, John","type":"item"},
{"id":9876543210123456,"uid":"9876543210123456","title":"Initial Review","type":"item"},
{"id":9876543210456789,"uid":"9876543210456789","title":"Final Check","type":"item"},
{"id":9876543210741852,"uid":"9876543210741852","title":"Prep Work","type":"item"},
{"id":9876543210963258,"uid":"9876543210963258","title":"Writing","type":"item"}]
}

I want to get the id based on taskName from another step. But I’m getting ‘Cannot read property ‘id’ of undefined’. What’s going wrong? Is it because of the array size? Any tips would be great!

Hey there! I’ve dealt with similar issues in Zapier before. The problem isn’t with the array size, but rather with the find() method not returning a match. This happens when no item in the array has a title that exactly matches inputData.taskName.

A few things to check:

Make sure inputData.taskName is exactly as it appears in the array. Check for any extra spaces or capitalization differences.

Consider using a more flexible matching method. Instead of strict equality, you could use toLowerCase() and includes() for a partial match:

TaskId: parsed.items.find(item => 
  item.title.toLowerCase().includes(inputData.taskName.toLowerCase())
).id

Add a fallback in case no match is found:

let matchedItem = parsed.items.find(item => item.title === inputData.taskName);
return {
  parsed,
  TaskId: matchedItem ? matchedItem.id : 'No match found'
};

Hope this helps you troubleshoot the issue!

yo, i’ve seen this before. the problem’s probs that ur inputData.taskName doesn’t match any title exactly. try using some fuzzy matching maybe?

u could do smth like:

TaskId: parsed.items.find(i => i.title.toLowerCase().includes(inputData.taskName.toLowerCase()))?.id || 'no match'

this’ll give u a partial match n won’t break if nothin’s found. hope it helps!

I’ve encountered this issue before in Zapier workflows. The problem likely stems from no exact match being found for inputData.taskName in your items array. To resolve this, you could implement a more robust error handling approach.

Consider wrapping your find operation in a try-catch block:

let taskId;
try {
  taskId = parsed.items.find(item => item.title === inputData.taskName).id;
} catch (error) {
  taskId = 'No matching task found';
}

return {
  parsed,
  TaskId: taskId
};

This way, if no match is found, you’ll get a clear error message instead of the undefined property error. It’s also worth double-checking that inputData.taskName is being correctly passed from the previous step and matches exactly with the titles in your items array.