[英]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.