node-red file-out(保存到文件)TypeError

时间:2018-03-27 08:00:37

标签: javascript node.js node-red

我希望在完成日志记录处理后将传入的json字符串(HTTP in)保存到文件中。

要保存文件,我使用文件节点(out)。

节点配置为追加到文件,如果不存在则创建目录。

该节点始终抛出异常TypeError: this is not a typed array

堆栈追踪:

  

“TypeError:这不是一个类型化的数组。在Function.from(native)↵at   filenode的。   (/usr/local/lib/node_modules/node-red/nodes/core/storage/50-file.js:67:39)↵   在emitOne(events.js:77:13)↵在FileNode.emit(events.js:169:7)↵at   FileNode.Node.receive   (/usr/local/lib/node_modules/node-red/red/runtime/nodes/Node.js:215:14)↵   在FunctionNode.Node.send   (/usr/local/lib/node_modules/node-red/red/runtime/nodes/Node.js:202:14)↵   在sendResults   (/usr/local/lib/node_modules/node-red/nodes/core/core/80-function.js:52:18)↵   在FunctionNode。   (/usr/local/lib/node_modules/node-red/nodes/core/core/80-function.js:205:21)↵   在emitOne(events.js:77:13)↵在FunctionNode.emit(events.js:169:7)“

有人可以解释一下我缺少的东西吗?我想要创建的文件不存在。就在文件节点之前,我设置msg.filename来创建唯一的文件名(时间戳)。 它只会创建文件夹(如果缺少),但永远不会创建文件。

我还尝试将一个简单的字符串而不是json对象设置为有效负载,但这也不起作用。

我发现了一些提示,我应该升级到最新的node.js和node-red。目前我正在使用node-red V 0.18.4和node.js V9.9.0

感谢您的帮助。

这是我当前的流程

[{"id":"e2d9bca5.4c3f78","type":"http in","z":"4961d970.3e0778","name":"send_data","url":"/incomingData","method":"post","upload":false,"swaggerDoc":"","x":110,"y":120,"wires":[["5400ed9e.a8353c"]]},{"id":"b0ad409a.8e9a1","type":"http response","z":"4961d970.3e0778","name":"HTTP Error Resonse","statusCode":"400","headers":{},"x":720,"y":380,"wires":[]},{"id":"b6620bad.3c7848","type":"file","z":"4961d970.3e0778","name":"error file","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"true","x":960,"y":400,"wires":[]},{"id":"16cac0d3.070297","type":"file","z":"4961d970.3e0778","name":"data file","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1060,"y":180,"wires":[]},{"id":"595e3863.fe83e","type":"http response","z":"4961d970.3e0778","name":"http success","statusCode":"200","headers":{},"x":850,"y":220,"wires":[]},{"id":"2cfbe34e.53a42c","type":"function","z":"4961d970.3e0778","name":"set filename for data","func":"var now = Date.now();\nvar filename = '/home/nodered/data/data_' + now + '.txt';\nmsg.filename = filename;\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":180,"wires":[["16cac0d3.070297"]]},{"id":"af847029.5c8968","type":"function","z":"4961d970.3e0778","name":"set filename for error","func":"var now = Date.now();\nvar filename = '/home/nodered/data/error_' + now + '.txt';\nmsg.filename = filename;\n\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":420,"wires":[["b6620bad.3c7848"]]},{"id":"eb0f3370.4e922","type":"catch","z":"4961d970.3e0778","name":"catch JSON parse errors","scope":["5400ed9e.a8353c"],"x":410,"y":180,"wires":[["4bb56ef4.7d1f2","b0ad409a.8e9a1","af847029.5c8968"]]},{"id":"4bb56ef4.7d1f2","type":"debug","z":"4961d970.3e0778","name":"Show error message in debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":750,"y":320,"wires":[]},{"id":"5400ed9e.a8353c","type":"json-schema-validator","z":"4961d970.3e0778","name":"check json string","func":"{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"id\": \"http://mynet.com/schemas/user.json#\",\n  \"title\": \"Data\",\n  \"description\": \"Data\",\n  \"type\": \"object\",\n  \"required\":[\"serialnr\", \"mappingcode\", \"data\"],\n  \"properties\": {\n    \"serialnr\": {\n      \"description\": \"Serial number of machine\",\n      \"type\": [\"string\"],\n      \"pattern\": \"^[1-9][0-9]*$\",\n      \"minLength\": 13,\n      \"maxLength\": 13\n    },\n    \"mappingcode\": { \"type\": \"integer\", \"maximum\": 99999},\n    \"yyyymmdd\": { \"type\": \"integer\", \"maximum\":20301231, \"minimum\":20150101 },\n    \"data\": {\n      \"type\": \"object\",\n      \"additionalProperties\": { \"type\": \"string\" },\n      \"maxProperties\": 200,\n      \"required\": [\"data1\",\"data2\"]\n    }\n \n }\n}","x":340,"y":120,"wires":[["2cfbe34e.53a42c","595e3863.fe83e"]]},{"id":"540ca799.905db8","type":"catch","z":"4961d970.3e0778","name":"","scope":["16cac0d3.070297","b6620bad.3c7848"],"x":1000,"y":480,"wires":[["83f0ce5e.11b378"]]},{"id":"83f0ce5e.11b378","type":"debug","z":"4961d970.3e0778","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1150,"y":480,"wires":[]}]

1 个答案:

答案 0 :(得分:0)

谢谢史蒂夫,

将fom 9.9.0降级回8.10.0之后我就可以创建文件了。