Short answer
Why Is My Zapier Code Step Returning Undefined?
A Code by Zapier step returns undefined or empty fields when the code never sets output or returns early. It also happens when an async call isn't awaited, or when the code reads an inputData key that doesn't exist or is blank in this run. Log the inputs, await every promise, and always return a plain object with the keys later steps need.
Common causes
- The code builds a result but never assigns it to
outputor returns it. - A
fetchor other promise isn't awaited, so the step finishes before the value exists. - The key in code (
inputData.email) doesn't match the Input Data name in the editor (Email). Keys are case-sensitive. - The mapped field is blank in this run, or arrives as a string you treat as a number or array.
- A property you return is
undefined, so it never shows up as a field for later steps.
Diagnostic steps
- Temporarily return the raw inputs, for example
output = {debug: JSON.stringify(inputData)}, and test the step. - Check that every key used in code exists in the Input Data list with exactly the same spelling.
- Put
awaitbefore everyfetchand.json()call, and checkresponse.okbefore reading the body. - Convert types on purpose: every
inputDatavalue is a string, so useNumber(),JSON.parse(), orsplit()as needed. - Return defaults instead of
undefined, such asoutput = {total: total ?? 0}, so later steps always get a field.
Example
A step calls an API with fetch(url).then(r => r.json()) but assigns output outside the callback. The step ends before the request finishes and returns nothing. Rewriting it as const data = await (await fetch(url)).json(); output = {id: data.id}; returns the ID.
Caveats
Test data can include fields that are blank in live runs, so test with a realistic record. Code steps have plan-based runtime and memory limits, so move long-running or heavy jobs to an external function.