catch missing template error in CSV parser node

and update help info
This commit is contained in:
Dave Conway-Jones 2015-10-14 23:38:29 +01:00
parent 61d7893467
commit 217c9718e4
3 changed files with 25 additions and 18 deletions

View File

@ -520,7 +520,8 @@
"windows": "Windows (\\r\\n)"
},
"errors": {
"csv_js": "This node only handles csv strings or js objects."
"csv_js": "This node only handles csv strings or js objects.",
"obj_csv": "No columns template specified for object -> csv."
}
},
"html": {

View File

@ -1,6 +1,6 @@
<!--
Copyright 2014 IBM Corp.
Copyright 2014,2015 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -68,12 +68,15 @@
</script>
<script type="text/x-red" data-help-name="csv">
<p>A function that parses the <b>msg.payload</b> to convert csv to/from a javascript object.
<p>A function that parses the <b>msg.payload</b> to convert CSV to/from a javascript object.
Places the result in the payload.</p>
<p>If the input is a string it tries to parse it as CSV and creates a javascript object.</p>
<p>If the input is a javascript object it tries to build a CSV string.</p>
<p>The columns template should contain an ordered list of column headers. For csv input these become the property names.
For csv output these specify the properties to extract from the object and the order for the csv.</p>
<p>If the input is a simple array the output is just a CSV generated from that array.</p>
<p>If the input is an array of arrays or an array of objects a multiple-line CSV is created.</p>
<p>The columns template should contain an ordered list of column headers. For CSV input these become the property names.
For CSV output these specify the properties to extract from the object and the order for the CSV.</p>
<p>If the input is an array then the columns template does not matter, but can be used to generate a row of column titles.</p>
<p><b>Note:</b> the columns should always be specified comma separated - even if another separator is chosen for the data.</p>
</script>

View File

@ -69,26 +69,29 @@ module.exports = function(RED) {
ou += msg.payload[s].join(node.sep) + node.ret;
}
else {
for (var t=0; t < node.template.length; t++) {
if (node.template[0] !== '') {
for (var t=0; t < node.template.length; t++) {
// aaargh - resorting to eval here - but fairly contained front and back.
var p = RED.util.ensureString(eval("msg.payload[s]."+node.template[t]));
// aaargh - resorting to eval here - but fairly contained front and back.
var p = RED.util.ensureString(eval("msg.payload[s]."+node.template[t]));
if (p === "undefined") { p = ""; }
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
p = p.replace(/"/g, '""');
ou += node.quo + p + node.quo + node.sep;
if (p === "undefined") { p = ""; }
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
p = p.replace(/"/g, '""');
ou += node.quo + p + node.quo + node.sep;
}
else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas"
ou += node.quo + p + node.quo + node.sep;
}
else { ou += p + node.sep; } // otherwise just add
}
else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas"
ou += node.quo + p + node.quo + node.sep;
}
else { ou += p + node.sep; } // otherwise just add
ou = ou.slice(0,-1) + node.ret; // remove final "comma" and add "newline"
}
ou = ou.slice(0,-1) + node.ret; // remove final "comma" and add "newline"
else { node.warn(RED._("csv.errors.obj_csv")); }
}
}
msg.payload = ou;
node.send(msg);
if (msg.payload !== '') { node.send(msg); }
}
catch(e) { node.error(e,msg); }
}