0

battling with what i think must be a trivial thing (and thus my trivial mistake - i m still new to node-red):

in node-red, trying to have a simple function to put payload such as [ 250, 8 ] into an array, perform a simple calculation, and return a result, like so:

var msgAll = { payload: msg.payload};
var msg0 = { payload :msg.payload[0] };
var msg1 = { payload :msg.payload[1] };
var msg2 = msg0 + msg1;
return [ msg0, msg1, msg2];

msg0 and msg1 get returned as number:

msg.payload : number
250
msg.payload : number
8

whereas msg2 is undefined

msg.payload : undefined
undefined

what am i missing about types, payloads, returning?

1 Answers1

2

You can not add 2 JavaScript objects together. If you want to add the 2 msg.payload values you need to explicitly add those 2 variables.

var msgAll = { payload: msg.payload};
var msg0 = { payload :msg.payload[0] };
var msg1 = { payload :msg.payload[1] };
var msg2 = {payload: msg0.payload + msg1.payload};
return [ msg0, msg1, msg2];
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • thanks, @hardillb, that clarified and solved it. the concept of object vs. the type of the object's payload still wasn't clear to me. – less.sebastian May 11 '18 at 16:27