Test if flow variable, i.e. context variable exists?

Test if flow variable, i.e. context variable exists?

I have tried...

if (flow.get('Test') || false) {
node.warn('Test Exist')
} else {
node.warn('Test Not Exist')
flow.set('Test', true)
}

But is this legit? The examples using type and 'undefined' all seem to illustrate simple variables, not context based.

So typeof does handle flow variables...

if (typeof flow.get('test') == 'undefined') {
flow.set('test', false)
node.warn('not exist')
} else {
node.warn('exist')
}

Might be an idea to add an example of this to the Context Variable use documentation?

This is nothing to do with context. Once you run flow.get('test') then you just have a javascript variable, so you can use typeof just the same as you would for any other variable.

What do you want to do when it is undefined? If you just want to give it a default value then you can use
let test = flow.get('test') ?? some_default_value
That will set test to the default value if it does not defined (or is null).

Ah "??" My lack of experience with JavaScript syntax bites me again. The lack of examples show flow variables in use versus a simple variable, implied to me that there was something special about... var A = flow.get('test') versus B = function() or such... which now seems nutty, but late last night? Well. But flow... is just what behind the curtain an object? Not a variable. Where 'get' is a JavaScript accessor I think it is called? Thanks for the tip on '??'... never seen that in Python! LOL.

The ?? operator is a fairly recent addition to javascript.

A variable is just a reference to a javascript object, or a javascript simple value. In the statement
let test = flow.get('test') ?? some_default_value
you are right that flow.get("test") is not literally a js variable, as it is not stored under an accessible name, it is the object, or value that the variable test will reference. However, when writing code, the syntax for using myVariable in an expression is exactly the same as using flow.get('test').

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.