Backend Variables - func1 initializes, func2 see null

I use a Web Module in the Backend with 2 functions and a variable.

I want to initialize the variable with one exported function and use it in the other function, both called from a Frontend page.

For some reason, I get null:


let item;

export function func1(objId) {
  try{
     return wixData.query("objects").eq("_id",objId)
        .find()
        .then(async (results) => {
             if (results.items.length > 0){
                item = results.items[0]; //init item
                let reservationObj = item;
                return reservationObj;
            }else{
                console.log("couldn't find object!!");
            }
        } ); 
    } catch (err) {
        console.log(err);
    }
}

export function func2(){
 return item._id === "some_id"; //at this point, item is null.
}

Any help?

Thanks!

Did you figure this out?

Y can use this or global. ‘this’ is an instance of global on node session.

set.jws
var global = require (“global”)
var document = require( “global/document” )
var window = require( “global/window” )

export function set() {
this.myVar = {obj: ‘json’}
global[‘var’] = ‘using global npm’
console.log(‘Set this’, this, this.myVar)
console.log(‘Set global’, global, global.var)
}

get.jws (another file)
export function get() {
console.log(‘Get this’, this, this.myVar)
console.log(‘Get global’, global, global.var)
}

Wix page1 code
import {set} from ‘backend/set’;

Wix page2 code
import {get} from ‘backend/get’;

You don’t need to import the npm module twice. You just need to import on first jsw file called.