cancel
Showing results for 
Search instead for 
Did you mean: 

Global variable troubles (Calling Variables Declared in Another Unit) in javascript

SOLVED
royd
Regular Contributor

Global variable troubles (Calling Variables Declared in Another Unit) in javascript

I have successfully used "//USEUNIT" method of calling functions and variables in the past.  I am trying to use "module.exports" method and can not figure out how  to make it work. Here is what I have -

 

The source unit:

 

//Patinet_Information
var nameOfPatient;
function patientStat(){
   ...
  nameOfPatient = page.FindChild("idStr", "patientName", 10).contentText;
   ...
 }

 module.exports.nameOfPatient;

 

 

The destination unit:

 

var patientInfo = require("Patinet_Information");
function createPatient(){
    ...
  let pName = page.FindChildByXPath("//input[@id='userName']");;
  pName.SetText(nameOfPatient);
}

I have also tried 

  pName.SetText(patientInfo.nameOfPatient);

 

 

did not work ("nameOfPatient is undefined").

 

Thanks in advance for any help.

 

Dave

1 ACCEPTED SOLUTION

Accepted Solutions
cunderw
Community Hero

Re: Global variable troubles (Calling Variables Declared in Another Unit) in javascript

Ah, I forgot the Javascript scope can be a little weird. You won't actually be able to reference that var directly without it being a const. 

 

So you could approach it two different ways:

 

let fooString = ""

function setFooString(){
 
 fooString = "Test";
 
}

function getFooString() {
  
  return fooString;
}

module.exports.setFooString = setFooString;
module.exports.getFooString = getFooString;
// bar file

let foo = require("foo");

function barFunction() { 
  foo.setFooString();
  ShowMessage(foo.getFooString());
}

Or you could do a const object like this:

 

// foo file
const info = {
  fooString : ""
}

function setFooString(){
 
 info.fooString = "Test";
 
}

module.exports.setFooString = setFooString;
module.exports.info = info;
// bar file

let foo = require("foo");

function barFunction() { 
  foo.setFooString();
  ShowMessage(foo.info.fooString);
}

I personally would prefer the second way because it's much easier to expand on what you're acceessing from the first unit.


Thanks,
Carson

Click the Accept as Solution button if my answer has helped

View solution in original post

5 REPLIES 5
cunderw
Community Hero

Re: Global variable troubles (Calling Variables Declared in Another Unit) in javascript

The first thing I see is that 

module.exports.nameOfPatient;

should be 

module.exports.nameOfPatient = nameOfPatient;

You have to specify what the export is referencing.  


Thanks,
Carson

Click the Accept as Solution button if my answer has helped
royd
Regular Contributor

Re: Global variable troubles (Calling Variables Declared in Another Unit) in javascript

Thank you. I will try that and report it when I get a chance.

royd
Regular Contributor

Re: Global variable troubles (Calling Variables Declared in Another Unit) in javascript

Hi cunderw

 

This morning corrected and tried your suggestion. I also realized that I missed a few things as well, so here is what I did -

//Patinet_Information
var nameOfPatient;
function patientStat(){
...
nameOfPatient = page.FindChild("idStr", "patientName", 10).contentText;
...
}

//module.exports.patientStat = patientStat;
module.exports.nameOfPatient = nameOfPatient;

and ...

 var Patinet_Information = require("Patinet_Information");
function createPatient(){
...
let pName = page.FindChildByXPath("//input[@id='userName']");;
pName.SetText(Patinet_Information.nameOfPatient);
}

  I am still getting nameOfPatinet 'undefined'.

cunderw
Community Hero

Re: Global variable troubles (Calling Variables Declared in Another Unit) in javascript

Ah, I forgot the Javascript scope can be a little weird. You won't actually be able to reference that var directly without it being a const. 

 

So you could approach it two different ways:

 

let fooString = ""

function setFooString(){
 
 fooString = "Test";
 
}

function getFooString() {
  
  return fooString;
}

module.exports.setFooString = setFooString;
module.exports.getFooString = getFooString;
// bar file

let foo = require("foo");

function barFunction() { 
  foo.setFooString();
  ShowMessage(foo.getFooString());
}

Or you could do a const object like this:

 

// foo file
const info = {
  fooString : ""
}

function setFooString(){
 
 info.fooString = "Test";
 
}

module.exports.setFooString = setFooString;
module.exports.info = info;
// bar file

let foo = require("foo");

function barFunction() { 
  foo.setFooString();
  ShowMessage(foo.info.fooString);
}

I personally would prefer the second way because it's much easier to expand on what you're acceessing from the first unit.


Thanks,
Carson

Click the Accept as Solution button if my answer has helped

View solution in original post

royd
Regular Contributor

Re: Global variable troubles (Calling Variables Declared in Another Unit) in javascript

Hi Carson

 

Thank you very much for your help. I will have to take a close look at it, I am relatively new to Javascript, still learning. 🙂

 

Thank you for taking the time to explain.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors