Node-red output a message to CSV

雨燕双飞 提交于 2019-12-25 08:48:08

问题


For the past couple of days I have been trying and reading to get something very specific done in Node-Red: I want to send the (LoRa) message to a CSV. This CSV should contain the following items:

  • topic
  • date
  • payload

I can insert the date using a function node:

var str1 = Date();

I have been playing around with CSV node, but I can't get it to output comma separated values. All this has probably to do with my lack of javascript programming skills, which is why I turn to you.

Can you help me out?

Edit: I'm still looking for the answer, which has brought me the following: Function node:

var res = Date() + "," + msg.topic + "," + msg.payload; return [ { payload: res } ]; 

Output:

[{"col1":"Mon Oct 17 2016 17:10:20 GMT+0200 (CEST)","col2":"1/test/1","col3":"string1"}]

All I want now is to lose the extra information such as column names and [{}]


回答1:


The CSV node works only on the msg.payload field so you will have to copy the extra data into the payload object to get it to output what you want.

So to format the data correctly you need to place a function node with the following before the CSV node:

var originalPayload = msg.payload;
var newPayload = {};
newPayload.date = new Date().toString();
newPayload.topic = msg.topic;
newPayload.payload = originalPayload;
msg.payload = newPayload;

return msg;

And configure the CSV node to output columns "date,topic,payload"



来源:https://stackoverflow.com/questions/40089644/node-red-output-a-message-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!