1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Change node: reparse JSON set value each time to avoid pass-by-ref

This commit is contained in:
Nick O'Leary 2016-10-09 23:14:52 +01:00
parent f22c3b549e
commit 0dd2c7fe24

View File

@ -21,8 +21,9 @@ module.exports = function(RED) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.rules = n.rules; this.rules = n.rules;
var rule;
if (!this.rules) { if (!this.rules) {
var rule = { rule = {
t:(n.action=="replace"?"set":n.action), t:(n.action=="replace"?"set":n.action),
p:n.property||"" p:n.property||""
} }
@ -39,7 +40,7 @@ module.exports = function(RED) {
var valid = true; var valid = true;
for (var i=0;i<this.rules.length;i++) { for (var i=0;i<this.rules.length;i++) {
var rule = this.rules[i]; rule = this.rules[i];
// Migrate to type-aware rules // Migrate to type-aware rules
if (!rule.pt) { if (!rule.pt) {
rule.pt = "msg"; rule.pt = "msg";
@ -76,7 +77,8 @@ module.exports = function(RED) {
rule.to = Number(rule.to); rule.to = Number(rule.to);
} else if (rule.tot === 'json') { } else if (rule.tot === 'json') {
try { try {
rule.to = JSON.parse(rule.to); // check this is parsable JSON
JSON.parse(rule.to);
} catch(e2) { } catch(e2) {
valid = false; valid = false;
this.error(RED._("change.errors.invalid-json")); this.error(RED._("change.errors.invalid-json"));
@ -90,6 +92,9 @@ module.exports = function(RED) {
try { try {
var property = rule.p; var property = rule.p;
var value = rule.to; var value = rule.to;
if (rule.tot === 'json') {
value = JSON.parse(rule.to);
}
var current; var current;
var fromValue; var fromValue;
var fromType; var fromType;