简体   繁体   English

如何在节点红色中增加msg.payload [i]

[英]How to increment msg.payload[i] in node-red

We are working on an ubuntu server, where we have installed node-red. 我们正在使用ubuntu服务器,已在其中安装了node-red。 What we want is to take data from one table on our MySQL Database, and then move it to another table. 我们想要的是从MySQL数据库的一个表中获取数据,然后将其移动到另一表中。

Our flow looks like this: 我们的流程如下所示:

在此处输入图片说明

Our 'Select Tolerance' node contains: 我们的“选择公差”节点包含:

msg.topic = "SELECT * FROM Tolerance";
return msg;

Simple code, which selects data from our database. 简单的代码,从我们的数据库中选择数据。 If we connect a debug node like this: 如果我们像这样连接调试节点:

在此处输入图片说明

We then see an output looking like this: 然后,我们看到如下所示的输出:

在此处输入图片说明

We want to pick all data, so we need to go through all objects in the array and make sure to take all values and send them over to our new database table. 我们希望选择所有数据,因此我们需要遍历数组中的所有对象,并确保获取所有值并将其发送到新的数据库表中。 We're using the 'New Tolerance' node to do so, and 'New Tolerance' contains: 我们使用“ New Tolerance”节点执行此操作,并且“ New Tolerance”包含:

var i;
var secured = 1;

for (i = 0; i < msg.payload.length; i++) {
    var time_start = msg.payload[i].time_start
    var time_end = msg.payload[i].time_end
    var temperatur_lpn = msg.payload[i].temperatur_lpn
    var temperatur_rising = msg.payload[i].temperatur_rising
    var temperatur_weather = msg.payload[i].temperatur_weather
    var temp_compare_WL = msg.payload[i].temp_compare_WL
    var temp_compare_WR = msg.payload[i].temp_compare_WR

var out = "INSERT INTO Sunrise_Tolerance (time_start, time_end, temperatur_lpn, temperatur_rising, temperatur_weather, temp_compare_WL, temp_compare_WR, secured)"

out = out + " VALUES ('" + time_start + "','" + time_end + "','" + temperatur_lpn + "','" + temperatur_rising + "','" + temperatur_weather + "','" + temp_compare_WL + "','" + temp_compare_WR + "','" + secured + "');"

msg.topic = out;
return msg;
}

The problem is that we only receive the first row of data (first object in the array) and not the rest. 问题在于我们只接收数据的第一行(数组中的第一个对象),而不接收其余的数据。 Can anyone figure out why we dont receive all data, but only the first? 谁能弄清楚为什么我们不接收所有数据,而仅接收第一个数据?

The problem comes the fact you have a return statement inside the for loop. 问题在于您在for循环中有一个return语句。

That will exit the function the first time it reaches that point - so your loop never loops. 这将在函数第一次到达该点时退出该函数-因此您的循环永远不会循环。

If you want to send multiple messages you should use node.send(msg); 如果要发送多个消息,则应使用node.send(msg); in your for loop. 在您的for循环中。 The only thing to watch out for is if you call node.send with the same object multiple times, as the message is passed by reference, you would get some odd side-effects. 唯一需要注意的是,如果多次调用同一对象的node.send ,由于消息是通过引用传递的,那么您会得到一些奇怪的副作用。 As you only care about msg.topic in this instance, you can afford to create a new message object each time. 由于您只在这种情况下关心msg.topic ,因此您可以每次创建一个新的消息对象。

So, instead of the return msg statement, you could do: 因此,您可以执行以下操作来代替return msg语句:


for (i ... ) {
   var out = "INSERT ...";
   ...

   node.send({topic: out});
}
// Return without any arguments so no further messages are sent.
return;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 node-red 中的 msg.payload 运行 javascript 函数 - run javascript function using msg.payload in node-red node-red 将 json 字符串解析为 msg.payload - node-red parsing a json string to msg.payload 您如何使用JavaScript在Node-Red中的msg.payload中解码HTML实体? - How would you decode HTML entities inside the msg.payload in Node-Red with javascript? 如何从节点红色仪表板模板发送msg.payload Int16Array - How to send msg.payload Int16Array from node-red dashboard template node-red 无法在模板节点和 JS 标签上获取 msg.payload - node-red cannot get msg.payload on template node unde JS tag Node-red:从 msg.payload 获取值并将其保存在变量中 - Node-red: get value from msg.payload and save it inside variable 在循环中的仪表板ui_template(node-red)中创建复选框,并将check-status分配给输出msg.payload - create checkbox in dashboard ui_template (node-red) in loop and assign check-status to output msg.payload 如何将 msg.payload 从注入节点移动到节点红色仪表板中的模板节点? - How to move msg.payload from inject node to template node in node red dashboard? 在节点红色中仅显示msg.payload中的值字段 - display only value fields from the msg.payload in node red 如何将msg.payload存储到ui_template节点红色内的脚本变量中? - How to store a msg.payload into a script variable inside a ui_template node red?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM