Reconstruct xml2js output as proper object

This commit is contained in:
Nick O'Leary 2023-05-22 13:57:12 +01:00
parent 46ae66c8b2
commit 6e1b298282
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
1 changed files with 21 additions and 1 deletions

View File

@ -33,7 +33,13 @@ module.exports = function(RED) {
parseString(value, options, function (err, result) {
if (err) { done(err); }
else {
value = result;
// TODO: With xml2js@0.5.0, they return an object with
// a null prototype. This could cause unexpected
// issues. So for now, we have to reconstruct
// the object with a proper prototype.
// Once https://github.com/Leonidas-from-XIV/node-xml2js/pull/674
// is merged, we can revisit and hopefully remove this hack
value = fixObj(result)
RED.util.setMessageProperty(msg,node.property,value);
send(msg);
done();
@ -46,4 +52,18 @@ module.exports = function(RED) {
});
}
RED.nodes.registerType("xml",XMLNode);
function fixObj(obj) {
const res = {}
const keys = Object.keys(obj)
keys.forEach(k => {
if (typeof obj[k] === 'object' && obj[k]) {
res[k] = fixObj(obj[k])
} else {
res[k] = obj[k]
}
})
return res
}
}