Problems with loop processing in Zapier webhooks

I’m encountering an issue with handling nested data in my Zapier webhook. The version I tested locally runs without any problems, but the one in Zapier triggers errors.

Local Test Code:

var requestData = {
    "url": "api-url-goes-here",
    "action": "FETCH",
    "data": {
        "fields": ["FullName", "emailAddress", "DateCreated"],
        "records": [
            ["john_doe", "[email protected]", "15 Mar, 2018 10:22:30"],
            ["sarahk", "[email protected]", "15 Mar, 2018 14:45:12"]
        ]
    }
};

var outputRecords = [];

for (var i = 0; i < requestData.data.records.length; i++) {
    var record = {};
    for (var j = 0; j < requestData.data.fields.length; j++) {
        record[requestData.data.fields[j]] = requestData.data.records[i][j];
    }
    outputRecords.push(record);
}

console.log(outputRecords);

Desired Output:

{
  "FullName": "john_doe",
  "emailAddress": "[email protected]",
  "DateCreated": "15 Mar, 2018 10:22:30"
}{
  "FullName": "sarahk",
  "emailAddress": "[email protected]",
  "DateCreated": "15 Mar, 2018 14:45:12"
}

Trouble with Zapier Code:

data_poll: function(bundle){
  var response = z.JSON.parse(bundle.response.content);
  var outputRecords = [];

  for (var i = 0; i < response.data.records.length; i++) {
     var record = {};
     for (var j = 0; j < response.data.fields.length; j++) {
        record[response.data.fields[j]] = response.records[i][j];
     }
     outputRecords.push(record);
  }
  return outputRecords || [];
}

I’m receiving the error: TypeError: Cannot read property 'records' of undefined. What could be causing this issue with my Zapier code?

Classic property path inconsistency error. Hit something similar with Zapier’s data parsing last year. The issue isn’t just the missing .data reference others mentioned - there’s also how Zapier handles response structure differently than your local environment. Zapier sometimes wraps or unwraps response data in weird ways. I’d add some defensive logging first: console.log(JSON.stringify(response)) at the top of your function to see exactly what structure you’re getting. Also throw in null checks like if (!response || !response.data || !response.data.records) before your loops. API response format sometimes changes slightly between local testing and Zapier’s execution environment, especially with webhook timing.

simple typo in your inner loop. you’ve got response.records[i][j] but it should be response.data.records[i][j] - you’re missing the .data part. you’re accessing fields correctly with response.data.fields but forgot to add .data for the record values. that’s why you’re getting the undefined error - response.records doesn’t exist.

Had this exact same issue a few months back with nested API responses in Zapier. Your problem is in the nested loop - you’re using response.records[i][j] instead of response.data.records[i][j]. You correctly access response.data.records.length and response.data.fields.length in your loop conditions, but then drop the .data when accessing the actual record values. That’s what’s causing the undefined error - response.records doesn’t exist, only response.data.records does. Change it to record[response.data.fields[j]] = response.data.records[i][j]; and you’re good. Spent hours debugging this same typo because it’s so easy to miss with deeply nested objects.