Node-RED 系列(3)-- Core nodes (Inject/Debug/Function)

The inject, debug and function node are the most commonly used of the Node-RED core nodes.
Core nodes are the ones that are part of the initial Node-RED install.

Inject

Injects a message into a flow either manually or at regular intervals. The message payload can be a variety of types, including strings, JavaScripts objects or the current time.

Outputs

payload
The configured payload of the message.
topic
An optional property that can be configured in the node.

Details

The Inject node can initiate a flow with a specific payload value. The default payload is a timestamp of the current time in millisecs since January 1st, 1970.

The node also supports injecting strings, numbers, booleans, JavaScript objects, or flow/global context values.

By default, the node is triggered manually by clicking on its button within the editor. It can also be set to inject at regular intervals or according to a schedule.

It can also be configured to inject once each time the flows are started.

The maximum Interval that can be specified is about 596 hours/ 24days. However if you are looking at intervals greater than one day you should consider using a scheduler node that can cope with power outages and restarts.
Note: The "Interval between times" and "at a specific time" options use the standard cron system. This means that 20 minutes will be at the next hour, 20 minutes past and 40 minutes past - not in 20 minutes time. If you want every 20 minutes from now - use the "interval" option.

Inject node

image.png
    1. trigger the inject manually
    1. debug messages
    1. topic - display the topic info
    1. payload - display the payload info

Debug

Displays selected message properties in the debug sidebar tab and optionally the runtime log. By default it displays msg.payload, but can be configured to display any property, the full message or the result of a JSONData expression.

Details

The debug sidebar provides a structured view of the messages it is sent, making it easier to understand their structure.
JavaScript objects and arrays can be collapsed and expanded as required. Buffer objects can be displayed as raw data or as a string if possible.

Alongside each message, the debug sidebar includes information about the time the message was received, the node that sent it and the type of the message. Clicking on the source node id will reveal that node within the workspace.

The button on the node can be used to enable or disable its output. It is recommended to disable or remove any Debug nodes that are not being used.

The node can also be configured to send all messages to the runtime log, or to send short (32 characters) to the status text under the debug node.


Debug node

Function

A JavaScript function to run against the messages being received by the node.

The messages are passed in as a JavaScript object called msg.

By convention it will have a msg.payload property containing the body of the message.

The function is expected to return a message object (or multiple message objects), but can choose to return nothing in order to halt a flow.

The Setup tab contains code that will be run whenever the node is started. The Close tab contains node that will be run whenever the node is stopped.

If an promise object is returned from the setup node, input message processing starts after its completion.

Details

See the online documentation for more information on writing functions.
Sending messages
The function can either return the messages it wants to pass on to the next nodes in the flow, or can call node.send(messages).
It can return/send:

  • a single message object - passed to nodes connected to the first output
  • an array of message objects - passed to nodes connected to the corresponding outputs

Note: The setup code is executed during the initialization of nodes. Therefore, if node.send is called in the setup tab, subsequent nodes may not be able to receive the message.
If any element of the array is itself an array of messages, multiple messages are sent to the corresponding output.
If null is returned, either by itself or as an element of the array, no message is passed on.

Logging and Error Handling

To log any information, or report an error, the following functions are available:

  • node.log("Log message")
  • node.warn("Warning")
  • node.error("Error")

The Catch node can also be used to handle errors. To invoke a Catch node, pass msg as a second argument to node.error.

node.error("Error",msg);

Accessing Node Information
In the function block, id and name of the node can be referenced using the following properties:

  • node.id - id of the node
  • node.name - name of the node

Using environment variables
Environment variables can be accessed using env.get("MY_ENV_VAR")

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Getting Started Use the Current Stable Version (7.1) Buil...
    Leonzai阅读 2,066评论 0 3
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,819评论 16 22
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    余生动听阅读 10,852评论 0 11
  • 在妖界我有个名头叫胡百晓,无论是何事,只要找到胡百晓即可有解决的办法。因为是只狐狸大家以讹传讹叫我“倾城百晓”,...
    猫九0110阅读 3,715评论 7 3
  • 彩排完,天已黑
    刘凯书法阅读 4,476评论 1 3

友情链接更多精彩内容