Skip to content

Switch#

使用 Switch 节点根据比较操作有条件地路由工作流。它类似于 IF 节点,但支持多个输出路径。

🌐 Use the Switch node to route a workflow conditionally based on comparison operations. It's similar to the IF node, but supports multiple output routes.

节点参数(Node parameters)#

选择节点应使用的模式

🌐 Select the Mode the node should use:

  • 规则:选择此模式为每个输出构建匹配规则。
  • 表达式:选择此模式以编写表达式,以编程方式返回输出索引。

节点配置取决于你选择的模式

🌐 Node configuration depends on the Mode you select.

规则(Rules)#

要使用此操作配置节点,请使用以下参数:

🌐 To configure the node with this operation, use these parameters:

  • 创建 路由规则 来定义比较条件。
    • 使用数据类型下拉菜单选择条件的数据类型和比较操作类型。例如,要创建某个日期之后的日期规则,请选择 日期和时间 > 之后
    • 根据你选择的数据类型和比较方式,条件中要输入的字段和值会有所不同。有关按数据类型列出的所有比较的完整列表,请参阅可用的数据类型比较
  • 重命名输出:打开此控件以重命名输出字段,将匹配的数据放入其中。输入你想要的输出名称

选择 添加路由规则 以添加更多规则。

🌐 Select Add Routing Rule to add more rules.

规则选项(Rule options)#

您可以使用这些选项进一步配置此操作的节点:

🌐 You can further configure the node with this operation using these Options:

  • 回退输出:当某个项目不符合任何规则或条件时,选择如何路由工作流。
    • :忽略此项。这是默认行为。
    • 额外输出:将项目发送到额外的独立输出。
    • 输出 0:将物品发送到与匹配第一条规则的物品相同的输出。
  • 忽略大小写:设置在评估条件时是否忽略字母大小写(开启)或强制区分字母大小写(关闭)。
  • 较宽松的类型验证:设置你是否希望 n8n 根据你选择的运算符尝试转换值类型(开启)或不尝试(关闭)。
  • 向所有匹配的输出发送数据:设置是否将数据发送到所有符合条件的输出(开启)或仅将数据发送到第一个符合条件的输出(关闭)。

表达式(Expression)#

要使用此操作配置节点,请使用以下参数:

🌐 To configure the node with this operation, use these parameters:

  • 输出数量:设置节点应具有的输出数量。
  • 输出索引:创建一个表达式来计算哪个输入项应被路由到哪个输出。该表达式必须返回一个数字。

模板和示例(Templates and examples)#

Template widget placeholder.

有关在 n8n 中使用条件创建复杂逻辑的更多信息,请参阅 使用条件进行分支

🌐 Refer to Splitting with conditionals for more information on using conditionals to create complex logic in n8n.

Available data type comparisons#

String#

String data type supports these comparisons:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is equal to
  • is not equal to
  • contains
  • does not contain
  • starts with
  • does not start with
  • ends with
  • does not end with
  • matches regex
  • does not match regex

Number#

Number data type supports these comparisons:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is equal to
  • is not equal to
  • is greater than
  • is less than
  • is greater than or equal to
  • is less than or equal to

Date & Time#

Date & Time data type supports these comparisons:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is equal to
  • is not equal to
  • is after
  • is before
  • is after or equal to
  • is before or equal to

Boolean#

Boolean data type supports these comparisons:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is true
  • is false
  • is equal to
  • is not equal to

Array#

Array data type supports these comparisons:

  • exists
  • does not exist
  • is empty
  • is not empty
  • contains
  • does not contain
  • length equal to
  • length not equal to
  • length greater than
  • length less than
  • length greater than or equal to
  • length less than or equal to

Object#

Object data type supports these comparisons:

  • exists
  • does not exist
  • is empty
  • is not empty