便捷方法#
n8n 提供以下方法以便在表达式中更轻松地执行常见任务。
Python 支持
您可以在代码节点中使用 Python,但表达式不支持该语言。
JavaScript
| 方法 | 描述 | 是否在代码节点中可用 |
|---|---|---|
$evaluateExpression(expression: string, itemIndex?: number) | 将字符串作为表达式求值。若未提供 itemIndex,n8n 将使用代码节点中第 0 项的数据。 | ✅ |
$ifEmpty(value, defaultValue) | $ifEmpty() 函数接收两个参数,检测第一个参数是否为空,然后返回该参数(若非空)或第二个参数(若第一个参数为空)。满足以下条件时第一个参数被视为空: * undefined * null * 空字符串 '' * value.length 返回 false 的数组 * Object.keys(value).length 返回 false 的对象 | ✅ |
$if() | $if() 函数接收三个参数:条件、满足条件时返回的值、未满足条 件时返回的值。 | ❌ |
$max() | 返回所提供数字中的最大值。 | ❌ |
$min() | 返回所提供数字中的最小值。 | ❌ |
Python
| 方法 | 描述 |
|---|---|
_evaluateExpression(expression: string, itemIndex?: number) | 将字符串作为表达式求值。若未提供 itemIndex,n8n 将使用代码节点中第 0 项的数据。 |
_ifEmpty(value, defaultValue) | _ifEmpty() 函数接收两个参数,检测第一个参数是否为空,然后返回该参数(若非空)或第二个参数(若第一个参数为空)。满足以下条件时第一个参数被视为空: * undefined * null * 空字符串 '' * value.length 返回 false 的数组 * Object.keys(value).length 返回 false 的对象 |