Sum function does not work with global variable.

325 views
Skip to first unread message

Jim Spooren

unread,
Nov 15, 2017, 8:43:55 AM11/15/17
to Node-RED
Hello, 

I am very new to node red but i'm trying to figure out how to calculate a sum with my global variable. For some reason it doesnt work. Also see the attached screenshot.

Result 1 gives the following result in the debugger: 25. 
Result 2 gives the following result in the debugger: "[object Object][object Object]"

Could someone please tell me what i'm doing wrong and/or give me an alternative to fix this? Thanks in advance.

the code in the function node:

var ObtainedAantalNee = global.get('GlobalVariableAantalNee');
var ObtainedAantalTotaleStemmen = global.get('GlobalVariableTotaalAantalStemmen');
//var Uitkomst = 0;

var test1 = 15;
var test2 = 10;


msg.payload = ObtainedAantalNee;
var AantalNeeKliks = msg.payload;

msg.payload = ObtainedAantalTotaleStemmen;
var AantalTotaleStemmen = msg.payload;



var Result1 = {payload: test1 + test2};
var Result2 = {payload: ObtainedAantalNee + ObtainedAantalTotaleStemmen};


return [AantalNeeKliks, AantalTotaleStemmen, Result1, Result2];


NodeRedProblem.PNG

Nick O'Leary

unread,
Nov 15, 2017, 8:50:43 AM11/15/17
to Node-RED Mailing List
How/where are you setting in global context?

It appears  'GlobalVariableAantalNee' and 'GlobalVariableTotaalAantalStemmen' are Objects and not numeric values you can simply add together.

The fact you return AantalNeeKliks and AantalTotaleStemmen from the Function which are actually the values of GlobalVariableAantalNee and GlobalVariableTotaalAantalStemmen, and the Function hasn't complained means they definitely are msg objects and not numeric values.

My guess is you've stored complete messages under each of those global properties, and its their 'payload' value you want to be adding. So something like the following would work:


var ObtainedAantalNee = global.get('GlobalVariableAantalNee');
var ObtainedAantalTotaleStemmen = global.get('GlobalVariableTotaalAantalStemmen');
msg.payload = ObtainedAantalNee.payload + ObtainedAantalTotaleStemmen.payload;
return msg;


Note that it is good to reuse the msg object the Function receives, rather than create new objects if you don't need need to.

Nick



--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/38ddd3c6-f217-4c12-bb3f-458dc4aa8289%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jim Spooren

unread,
Nov 16, 2017, 4:59:35 AM11/16/17
to Node-RED
Thank you so much for your quick response! This works like a charm, the .payload did it :) thanks again.

Op woensdag 15 november 2017 14:50:43 UTC+1 schreef Nick O'Leary:
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages