TypeError: context.global.get is not a function

3,167 views
Skip to first unread message

Luiey

unread,
May 3, 2016, 5:49:54 AM5/3/16
to Node-RED

Guys,

What is lack in here? I'm trying to follow every instruction on how to hold a value for next incoming injection?

The value from modbus is handling in function node to receive and parse every buffer to get a value. Now the value will be deduct with "temp" variable value which if not the object is created, it will created new object of value to 0.

So, I'm putting a another node "context" to declare a context global with value 0 as initialize. And sending the value to node "function". What I'm intend to do is:-

- Declare a new variable/object if not created with default value of 0 called "temp".
- Read buffer from modbus and parse to get the needed value called "curr_value.
- Deduct "curr_value - temp = new_value" and store "new_value" in a global/any temporary storage for next interval.

I have tried below code but debug panel throw: TypeError: context.global.get is not a function on my "context" function node.
// initialise the counter to 0 if it doesn't exist already
var count = context.global.get("count")||0;
// store the value back
context
.global.set("count",count);
// make it part of the outgoing msg object
msg
.count = count;

On my function node:-
var temp = msg.count;
var result = msg.payload.result.map(x => parseInt(x,16));
var curr_value = result[1];
var new_value= value-temp;
context
.global.set('count',new_value);

Any idea? I have try referencing below link but same result "xxxx is not a function":-
1. Lecture 6: Intermediate Flow
2. Node-RED: Storing Data
3. Node-RED: Context



Nicholas O'Leary

unread,
May 3, 2016, 5:54:38 AM5/3/16
to Node-RED Mailing List
Hi,

please refer to the docs on using context in Function nodes here: http://nodered.org/docs/writing-functions#global-context

The docs you link to are for node implementations - not Function nodes.

You should use the .get/.set methods on the 'global' object, not context.global.

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+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Luiey

unread,
May 3, 2016, 10:12:46 PM5/3/16
to Node-RED
Hi,

I have open the Settings.js file from the folder that located together with red.js, package.json, README.md

    functionGlobalContext: {
       
// os:require('os')
       
// fs: require("fs")
       
// bonescript:require('bonescript'),
       
// jfive:require("johnny-five"),
       
// j5board:require("johnny-five").Board({repl:false})
   
},

At above section is what you mention right for declaring the globalcontext. So, I'm uncomment to be as below:-

    functionGlobalContext: {
        os
:require('os')
       
// fs: require("fs")
       
// bonescript:require('bonescript'),
       
// jfive:require("johnny-five"),
       
// j5board:require("johnny-five").Board({repl:false})
   
},

Am I right? So I'm applying the code in the first function node:-
// initialise the counter to 0 if it doesn't exist already
var count = context.global.get("lastkwh")||0;

// store the value back

context
.global.set("lastkwh",count);

// make it part of the outgoing msg object
return msg;
First will direct wired to second function node.

This is from my second function node:-
var temp = context.global.get("lastkwh");

var result = msg.payload.result.map(x => parseInt(x,16));
var dev1 = result[1];
var curr_kwh = dev1-temp;
context
.global.set('lastkwh',curr_kwh);

If anyone know what I'm wrong here, kindly correct me. I still get TypeError: context.global.get is not a function

Deon Pearson

unread,
May 3, 2016, 11:06:45 PM5/3/16
to Node-RED
Hi,

As Nick mentioned above, use only global.get & global.set
In your case above use: global.get("lastkwh") and global.set("lastkwh",curr_kwh)

Deon

Luiey

unread,
May 3, 2016, 11:46:31 PM5/3/16
to Node-RED
Hah. I don't know what is wrong here. Lol! I'm following this link issue and got it work finally. Look same one like I did but seems differ.

link here: https://github.com/node-red/node-red/issues/405
Reply all
Reply to author
Forward
0 new messages