You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 inputData for inputs, not DOM elements
    You can't access document in Zapier's code tool—instead, you'll pass weight and height as input fields, which are available via the inputData object (e.g., inputData.weight).
  • You must define an output variable
    Zapier requires you to explicitly set an output object 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 like alert() and updating form fields won't work here—we'll replace those with logic that returns error messages directly in the output.

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:

  1. When setting up the Code by Zapier step, add two input fields: weight (set to number type) and height (set to number type)
  2. Enter test values (e.g., weight=70, height=175) before running the test
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:14:59