加载 zip 内容(函数执行 2 次)
Load zip content (function executes 2 times)
我想获取带有func节点的zip文件内容如下,但是不知道为什么这个节点要执行两次
我哪里做错了?请告诉我!
var JSZip = global.get('JSZip');
var zip = new JSZip()
msg.isOK = false;
zip.loadAsync(msg.payload).then(function () {
// read files
const promises = [];
zip.forEach(function (path, zFile) {
promises.push(zFile.async('string').then(function (content) {
return {
filename: path,
payload: content
};
}));
});
return Promise.all(promises);
}).then(function (files) {
// send the result
msg.payload = files;
msg.isOK = true;
send(msg);
done();
}).catch(function (err) {
});
return msg;
流量:
[{"id":"7f6426e8.808bb8","type":"file in","z":"f63ce9bd.5c0d28","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":3710,"y":140,"wires":[["679b95b2.ce366c"]]},{"id":"c1e50527.9abf88","type":"change","z":"f63ce9bd.5c0d28","name":"DataTest.zip","rules":[{"t":"set","p":"filename","pt":"msg","to":"D:\Work\DataTest.zip","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3570,"y":140,"wires":[["7f6426e8.808bb8"]]},{"id":"679b95b2.ce366c","type":"function","z":"f63ce9bd.5c0d28","name":"Load Zip Content","func":"// variable declaration\nvar JSZip = global.get('JSZip');\nvar zip = new JSZip()\nmsg.isOK = false;\n\nzip.loadAsync(msg.payload).then(function () {\n // read files\n const promises = [];\n zip.forEach(function (path, zFile) {\n promises.push(zFile.async('string').then(function (content) {\n return {\n filename: path,\n payload: content\n };\n }));\n });\n\n return Promise.all(promises);\n}).then(function (files) {\n // send the result\n msg.payload = files;\n msg.isOK = true;\n send(msg);\n done();\n}).catch(function (err) {\n});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":3870,"y":140,"wires":[["2b710ce5.651694"]]},{"id":"7ebcebca.c16d84","type":"inject","z":"f63ce9bd.5c0d28","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":3430,"y":140,"wires":[["c1e50527.9abf88"]]},{"id":"f941bb77.168fb8","type":"debug","z":"f63ce9bd.5c0d28","name":"Error","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":4150,"y":200,"wires":[]},{"id":"2b710ce5.651694","type":"switch","z":"f63ce9bd.5c0d28","name":"","property":"isOK","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":4030,"y":140,"wires":[["b719c021.92cbe"],["f941bb77.168fb8"]]},{"id":"b719c021.92cbe","type":"debug","z":"f63ce9bd.5c0d28","name":"OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":4150,"y":80,"wires":[]}]
您正在呼叫 send(msg)
和 return msg
这意味着您从函数节点发送了 2 条消息,并且由于 NodeJS/JavaScript 是通过引用传递的,因此它们最终都将具有相同的内容。
最好的选择就是删除函数末尾的 return msg
。
我想获取带有func节点的zip文件内容如下,但是不知道为什么这个节点要执行两次
我哪里做错了?请告诉我!
var JSZip = global.get('JSZip');
var zip = new JSZip()
msg.isOK = false;
zip.loadAsync(msg.payload).then(function () {
// read files
const promises = [];
zip.forEach(function (path, zFile) {
promises.push(zFile.async('string').then(function (content) {
return {
filename: path,
payload: content
};
}));
});
return Promise.all(promises);
}).then(function (files) {
// send the result
msg.payload = files;
msg.isOK = true;
send(msg);
done();
}).catch(function (err) {
});
return msg;
流量:
[{"id":"7f6426e8.808bb8","type":"file in","z":"f63ce9bd.5c0d28","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":3710,"y":140,"wires":[["679b95b2.ce366c"]]},{"id":"c1e50527.9abf88","type":"change","z":"f63ce9bd.5c0d28","name":"DataTest.zip","rules":[{"t":"set","p":"filename","pt":"msg","to":"D:\Work\DataTest.zip","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3570,"y":140,"wires":[["7f6426e8.808bb8"]]},{"id":"679b95b2.ce366c","type":"function","z":"f63ce9bd.5c0d28","name":"Load Zip Content","func":"// variable declaration\nvar JSZip = global.get('JSZip');\nvar zip = new JSZip()\nmsg.isOK = false;\n\nzip.loadAsync(msg.payload).then(function () {\n // read files\n const promises = [];\n zip.forEach(function (path, zFile) {\n promises.push(zFile.async('string').then(function (content) {\n return {\n filename: path,\n payload: content\n };\n }));\n });\n\n return Promise.all(promises);\n}).then(function (files) {\n // send the result\n msg.payload = files;\n msg.isOK = true;\n send(msg);\n done();\n}).catch(function (err) {\n});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":3870,"y":140,"wires":[["2b710ce5.651694"]]},{"id":"7ebcebca.c16d84","type":"inject","z":"f63ce9bd.5c0d28","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":3430,"y":140,"wires":[["c1e50527.9abf88"]]},{"id":"f941bb77.168fb8","type":"debug","z":"f63ce9bd.5c0d28","name":"Error","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":4150,"y":200,"wires":[]},{"id":"2b710ce5.651694","type":"switch","z":"f63ce9bd.5c0d28","name":"","property":"isOK","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":4030,"y":140,"wires":[["b719c021.92cbe"],["f941bb77.168fb8"]]},{"id":"b719c021.92cbe","type":"debug","z":"f63ce9bd.5c0d28","name":"OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":4150,"y":80,"wires":[]}]
您正在呼叫 send(msg)
和 return msg
这意味着您从函数节点发送了 2 条消息,并且由于 NodeJS/JavaScript 是通过引用传递的,因此它们最终都将具有相同的内容。
最好的选择就是删除函数末尾的 return msg
。