Accessing global variables from a custom node

#1

Hello,
i recently managed to create my own node but unfortunately i can't access my global variables in the JavaScript Code of the node. I tried it like this: global.get("name")
As soon as i use it in Node-RED i get the following Error:
"TypeError: global.get is not a function"

Does somebody know how to do it in the right way?
Thank you in advance!

0 Likes

#2

As per the docs? https://nodered.org/docs/creating-nodes/context

0 Likes

#3

Thats my code:
module.exports = function(RED) {
function test11(config) {
RED.nodes.createNode(this,config);
var context = this.context();
var globalContext = this.context().global;
var node = this;
this.on('input', function(msg) {

    var outMsg = {};
	
	var path = msg.path;
	var iteration = msg.iteration;

	outMsg.headers = {};
	outMsg.headers['Authorization'] = 'Basic '+ global.get("encodedWithName");

What am I doing wrong ?

0 Likes

#4

you name the global context globalContext not global. So you need globalContext.get("encodedWithName")

0 Likes

#5

When sharing code on the forum, please follow the advice in this post: How to share code or flow json

0 Likes

#6

Thank you very much!

0 Likes

#7

thanks for the advice

0 Likes