Zapier中JavaScript脚本输出字段值报错,新手求助修改BMI代码
Fixing Your Zapier BMI Script Error
Hey there! Let's work through this issue together—you're hitting two key problems here: your code is written for browser JavaScript (using DOM elements like document.bmiForm) but Zapier runs code in a Node.js server environment, and you're missing the mandatory output variable Zapier requires to return results.
Let's break down the fixes:
- Zapier uses
inputDatafor inputs, not DOM elements
You can't accessdocumentin Zapier's code tool—instead, you'll pass weight and height as input fields, which are available via theinputDataobject (e.g.,inputData.weight). - You must define an
outputvariable
Zapier requires you to explicitly set anoutputobject with the data you want to pass to the next step in your zap. This is exactly what the error message is calling out! - Remove browser-only code
Things likealert()and updating form fields won't work here—we'll replace those with logic that returns error messages directly in theoutput.
Here's the revised code that works in Zapier:
// Get input values from Zapier's configured input fields const weight = parseFloat(inputData.weight); const height = parseFloat(inputData.height); // Initialize an object to hold our results let result = {}; // Validate input values first if (isNaN(weight) || weight <= 0 || isNaN(height) || height <= 0) { result.error = "Por favor preencha os dados corretamente"; } else { // Calculate BMI (using Math.pow for cleaner square calculation) const finalBmi = weight / Math.pow(height / 100, 2); result.bmi = parseFloat(finalBmi.toFixed(2)); // Round to 2 decimal places for readability // Determine BMI category meaning if (finalBmi < 18.5) { result.meaning = "Você está muito magro."; } else if (finalBmi <= 25) { result.meaning = "Você está saudável."; } else { result.meaning = "Você está com sobrepeso."; } } // Mandatory: Assign our result to Zapier's required output variable output = result;
How to test this in Zapier:
- When setting up the Code by Zapier step, add two input fields:
weight(set to number type) andheight(set to number type) - Enter test values (e.g., weight=70, height=175) before running the test
- The output will return either the calculated BMI + category, or an error message if your inputs are invalid
This code aligns with Zapier's requirements, strips out browser-specific logic, and clearly returns the results you need to use in subsequent zap steps.
内容的提问来源于stack exchange,提问作者Querini
相关产品推荐
相关产品推荐

