I have a working piece of code that processes data using nested for loops. It performs well when I run it locally, but when I try to use the same logic in my Zapier webhook handler, I encounter errors indicating that certain properties are undefined.
Here’s the structure of my test data:
var data = {
"endpoint": "api/getData",
"method": "FETCH",
"output": {
"headers": ["Username", "contact", "Created"],
"records": [
["john1", "[email protected]", "15 Jan, 2018 10:25:30"],
["mary2", "[email protected]", "15 Jan, 2018 14:20:15"]
]
}
};
var output = [];
for (var x = 0; x < data.output.records.length; x++) {
var item = {};
for (var y = 0; y < data.output.headers.length; y++) {
item['"' + data.output.headers[y] + '"'] = data.output.records[x][y];
}
output.push(item);
}
console.log(output);
This generates the output I expect. However, when I modify it for use in Zapier, I receive the error TypeError: Cannot read property 'records' of undefined. Here’s how my Zapier function looks:
process_webhook: function(bundle) {
var data = z.JSON.parse(bundle.response.content);
var output = [];
for (var x = 0; x < data.output.records.length; x++) {
var item = {};
for (var y = 0; y < data.output.headers.length; y++) {
item['"' + data.output.headers[y] + '"'] = data.records[x][y];
}
output.push(item);
}
return output || [];
}
What might be causing this discrepancy in performance between my local testing and the Zapier environment?