Function 节点详细说明

Function 节点详细说明

function 节点可以让我们使用js对传入的msg进行处理,然后返回一条会多条消息,以使后续流程继续执行.
传入的消息通常以对象形式,例:{}, 其中包含msg.payload msg.topic msg._msgid, msg.payload用来保存消息的主体内容。

编写函数

正常我们新建一个 function节点的时候,最简单的功能就是把收到的消息直接返回,其函数主题如下:

return msg;

如果返回null,则意味没有消息传出,流程将被终止,不会继续执行下去

返回的消息不必要和传入的消息一致,function可以构建一个全新的对象返回,例如:

var newMsg= {payload : 'Hello World'}
return newMsg

注意: 构造一个全新的消息对象,会丢失所接收消息的所有属性,并且造成某些流程中断。比如对于使用HTTP In/Response的流程而言,要求msg.req和msg.res在端到端的传输过程中一致保留。通常情况下,函数节点应该返回它们所接收到的完整消息对象,只改变其中的某些特性。

多输出端口

在之前switch节点的时候,我们发现输出端口是可以存在多个的,那么我们自己如何控制呢?

{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"905d59f0.fd9cd8","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"[1,2,3,4,5]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":290,"wires":"922ea77f.319458"]]},{"id":"a0f0fa50.f84fc8","type":"debug","z":"68360605.862db8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":633,"y":267,"wires":]},{"id":"7a5c486c.4d8968","type":"function","z":"68360605.862db8","name":"","func":"if (msg.topic === \"banana\") {\n   return  null, msg ];\n} else {\n   return  msg, null ];\n}","outputs":2,"noerr":0,"x":464,"y":285,"wires":"a0f0fa50.f84fc8"],"adb1168d.409ab8"]]},{"id":"adb1168d.409ab8","type":"debug","z":"68360605.862db8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":636,"y":317,"wires":]},{"id":"922ea77f.319458","type":"change","z":"68360605.862db8","name":"","rules":{"t":"set","p":"topic","pt":"msg","to":"banana","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":308,"y":284,"wires":"7a5c486c.4d8968"]]}]

日志

在我们写自己想写的功能是,想知道中间某些字段的信息可以通过打印日志, 实现方法如下:

node.log("xxx")
node.warn("xxxx")
node.error("xxx")

warnerror输出的信息在调试面板中可以看到

错误处理

function 节点在执行过程中如果发生了错误造成流程停止,将不会有任何结果输出,为了我们方便在面板查看信息,应该调用node.error,并将原始消息作为第二个参数

node.error("发生错误", msg)

保存数据

function节点中你有三种方式可以保存数据,需要根据环境自行选择,注意:Node Red重新部署context存储的这些数据将会消失, flow global 服务重启存储的数据将会丢失

  • context 保存的数据当前流程中可以获取到
  • flow 当前面板中都可以获取到保存的数据
  • global 所有面板中都可以获取到保存的数据

{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"c3cfc825.cfe2f8","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":512,"y":332,"wires":]},{"id":"dd464ce8.2453f","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":65,"y":335,"wires":"4b3398d6.d012d8"]]},{"id":"4b3398d6.d012d8","type":"function","z":"68360605.862db8","name":"","func":"// 如果count不存在则将其初始化为0\nvar count = context.get('count')||0;\ncount += 1;\n// 保存数据\ncontext.set('count',count);\n// 将其作为输出消息的一部分\nmsg.count = count;\nreturn msg;","outputs":1,"noerr":0,"x":231,"y":338,"wires":"c3cfc825.cfe2f8"]]},{"id":"af7ff661.930268","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":504,"y":443,"wires":]},{"id":"41852a4f.fc92b4","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":74,"y":446,"wires":"f1d77d46.60514"]]},{"id":"f1d77d46.60514","type":"function","z":"68360605.862db8","name":"","func":"// 如果count不存在则将其初始化为0\nvar count = context.get('count')||0;\ncount += 1;\n// 保存数据\ncontext.set('count',count);\n// 将其作为输出消息的一部分\nmsg.count = count;\nreturn msg;","outputs":1,"noerr":0,"x":223,"y":449,"wires":"af7ff661.930268"]]}]
{"id":"c3cfc825.cfe2f8","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":512,"y":332,"wires":]},{"id":"dd464ce8.2453f","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":94,"y":341,"wires":"4b3398d6.d012d8"]]},{"id":"4b3398d6.d012d8","type":"function","z":"68360605.862db8","name":"","func":"// 如果count不存在则将其初始化为0\nvar count = flow.get('count')||0;\ncount += 1;\n// 保存数据\nflow.set('count',count);\n// 将其作为输出消息的一部分\nmsg.count = count;\nreturn msg;","outputs":1,"noerr":0,"x":231,"y":338,"wires":"c3cfc825.cfe2f8"]]},{"id":"af7ff661.930268","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":504,"y":443,"wires":]},{"id":"41852a4f.fc92b4","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":74,"y":446,"wires":"f1d77d46.60514"]]},{"id":"f1d77d46.60514","type":"function","z":"68360605.862db8","name":"","func":"// 如果count不存在则将其初始化为0\nvar count = flow.get('count')||0;\ncount += 1;\n// 保存数据\nflow.set('count',count);\n// 将其作为输出消息的一部分\nmsg.count = count;\nreturn msg;","outputs":1,"noerr":0,"x":251,"y":446,"wires":"af7ff661.930268"]]}]

增加状态

function节点也可以像其他节点那样,显示一个当前节点的状态, 使用node.status,任何变化都会在节点上显示 例如:

node.status({fill:"red",shape:"ring",text:"disconnected"});
node.status({fill:"green",shape:"dot",text:"connected"});
node.status({text:"Just text status"});
node.status({});
1 个赞

感谢花神,学习了

花神你好,请问gif这个节点怎么使用,能不能提供一个流