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

Treat missing msg properties as undefined rather than throw error

Fixes #1167
This commit is contained in:
Nick O'Leary 2017-03-02 14:02:26 +00:00
parent d25dac69d2
commit 5945be95cf
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 36 additions and 2 deletions

View File

@ -125,7 +125,11 @@ module.exports = function(RED) {
return; return;
} }
} else { } else {
v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg); try {
v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
} catch(err) {
v1 = undefined;
}
} }
v2 = rule.v2; v2 = rule.v2;
if (rule.v2t === 'prev') { if (rule.v2t === 'prev') {
@ -138,7 +142,11 @@ module.exports = function(RED) {
return; return;
} }
} else if (typeof v2 !== 'undefined') { } else if (typeof v2 !== 'undefined') {
v2 = RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg); try {
v2 = RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg);
} catch(err) {
v2 = undefined;
}
} }
if (rule.t == "else") { test = elseflag; elseflag = true; } if (rule.t == "else") { test = elseflag; elseflag = true; }
if (operators[rule.t](test,v1,v2,rule.case)) { if (operators[rule.t](test,v1,v2,rule.case)) {

View File

@ -347,6 +347,32 @@ describe('switch Node', function() {
switchNode1.receive({payload:undefined}); switchNode1.receive({payload:undefined});
}); });
}); });
it('should treat non-existant msg property conditional as undefined', function(done) {
var flow = [{"id":"switchNode1","type":"switch","z":"feee1df.c3263e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"this.does.not.exist","vt":"msg"}],"checkall":"true","outputs":1,"x":190,"y":440,"wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
var received = [];
helperNode1.on("input", function(msg) {
received.push(msg);
});
// First message should be dropped as payload is not undefined
switchNode1.receive({topic:"messageOne",payload:""});
// Second message should pass through as payload is undefined
switchNode1.receive({topic:"messageTwo",payload:undefined});
setTimeout(function() {
try {
received.should.have.lengthOf(1);
received[0].should.have.a.property("topic","messageTwo");
done();
} catch(err) {
done(err);
}
},100)
});
});
it('should check if input is indeed not null', function(done) { it('should check if input is indeed not null', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"nnull"}],checkall:false,outputs:1,wires:[["helperNode1"]]}, var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"nnull"}],checkall:false,outputs:1,wires:[["helperNode1"]]},