Search code examples
node-red

How to retrieve a variable from the object msg?


I'm new to node-red and I've been trying to create a new variable following the example of the doc http://nodered.org/docs/writing-functions.html
Here's the code :

// initialise the counter to 0 if it doesn't exist already
var count = context.get('count')||0;
count += 1;
// store the value back
context.set('count',count);
// make it part of the outgoing msg object
msg.count = count;
return msg;

But then I have trouble using the variable msg.count. I've made a message trying to print the value with multiple attempts such as {msg.count} or {msg.get('count')} but it writes undefined or nothing each time.
How can I print the variable count?
This is what the flow looks like, the switch simply checking the value of msg.count.

enter image description here


Solution

  • I was trying too complicated.
    I simply had to use {count} instead of {msg.count}.