跳到主要内容

getWorkflowStaticData(type)#

此方法用于访问静态工作流数据。

实验性功能

  • 测试工作流时无法使用静态数据。工作流必须处于活跃状态,并通过触发器或网页钩子调用才能保存静态数据。
  • 在高频工作流执行场景下,此功能可能表现不稳定。

您可以直接在工作流中保存数据。此类数据体量应当较小。

例如:您可以保存从RSS订阅源或数据库处理的最后条目的时间戳。该方法始终返回对象。随后可对该对象的属性进行读取、删除或设置操作。当工作流执行成功时,n8n会自动检测数据是否发生变化,并在必要时保存数据。

静态数据分为两种类型:全局数据和节点数据。全局数据在整个工作流中保持一致,工作流中的每个节点均可访问。节点数据具有节点专属性,仅设置该数据的节点可再次读取。

全局数据示例:

JavaScriptPython
// Get the global workflow static data const workflowStaticData = $getWorkflowStaticData('global'); // Access its data const lastExecution = workflowStaticData.lastExecution; // Update its data workflowStaticData.lastExecution = new Date().getTime(); // Delete data delete workflowStaticData.lastExecution;# Get the global workflow static data workflowStaticData = _getWorkflowStaticData('global') # Access its data lastExecution = workflowStaticData.lastExecution # Update its data workflowStaticData.lastExecution = new Date().getTime() # Delete data delete workflowStaticData.lastExecution

包含节点数据的示例:

JavaScriptPython
// Get the static data of the node const nodeStaticData = $getWorkflowStaticData('node'); // Access its data const lastExecution = nodeStaticData.lastExecution; // Update its data nodeStaticData.lastExecution = new Date().getTime(); // Delete data delete nodeStaticData.lastExecution;# Get the static data of the node nodeStaticData = _getWorkflowStaticData('node') # Access its data lastExecution = nodeStaticData.lastExecution # Update its data nodeStaticData.lastExecution = new Date().getTime() # Delete data delete nodeStaticData.lastExecution

模板与示例#

查看模板详情