How to Define Output Variables in Zapier JavaScript Code

I’m having trouble with JavaScript in Zapier and need some help figuring out how to properly set output values.

I’m just starting out with Zapier automation and trying to learn JavaScript at the same time. I found a weight calculator script online and wanted to modify it to work with Zapier, but I keep getting this error message:

Error: You did not define output! Try output = {id: 1, hello: "world"};

Here’s the code I’m working with:

// Weight calculator function
var computeWeight = function computeWeight() {
    var mass = document.weightForm.mass.value
    var tall = document.weightForm.tall.value
    if (mass > 0 && tall > 0) {
        var result = mass / (tall / 100 * tall / 100)
        document.weightForm.result.value = result
        if (result < 18.5) {
            document.weightForm.status.value = "Underweight category"
        }
        if (result > 18.5 && result < 25) {
            document.weightForm.status.value = "Normal weight range"
        }
        if (result > 25) {
            document.weightForm.status.value = "Overweight category"
        }
    }
    else {
        alert("Please enter valid numbers")
    }
}

Can someone explain what I need to change in my code to make it work properly with Zapier? I’m not sure how to structure the output correctly.

your code’s tryin to access DOM elements that dont exist in zapier. you need to grab input values from inputData and return an output object instead. try var mass = inputData.mass; var tall = inputData.tall; then finish with output = {result: result, status: status};

Your code was written for a web browser, but Zapier’s JavaScript runtime doesn’t have document or DOM manipulation. There’s no webpage - you need to work with Zapier’s input/output system instead. Here’s the fix: Replace DOM references with inputData to grab your values, then create an output object at the end. javascript var mass = inputData.mass; var tall = inputData.tall; if (mass > 0 && tall > 0) { var result = mass / (tall / 100 * tall / 100); var status = ""; if (result < 18.5) { status = "Underweight category"; } else if (result >= 18.5 && result < 25) { status = "Normal weight range"; } else { status = "Overweight category"; } output = {bmi: result, category: status}; } else { output = {error: "Please enter valid numbers"}; } The key difference: use inputData for inputs and create an output object that Zapier can pass to the next step.

Your JavaScript is written for browsers where it can grab form data through the document object. Zapier’s environment doesn’t have DOM access or browser APIs. You need to restructure this to work with Zapier’s data flow. Instead of reading form fields, grab your data from the inputData object Zapier gives you. Your calculation logic is fine - just store the results in variables and assign them to the output object at the end. Use var mass = inputData.mass; var tall = inputData.tall; to get inputs, then after calculations do output = {bmi_result: result, weight_status: status}; to return data. Make sure every code path sets the output variable, including errors.