Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a function located in typescript file A from another typescript file B

I have the following code that is not executing properly

platform.tsx

  import { windowHelper } from "./windowHelper";
import { officeHelper } from "./officeHelper";
import { googleHelper } from "./googleHelper";
export class platformHelper {
 
    static callFCT = (fnctname: any, fnctparams = null) => {
        const platform = window.localStorage ? window.localStorage.getItem('platform') : "office";
        var fn: any = null;
        var wndhelper:any = new windowHelper();
        var offhelper:any = new officeHelper();
        var gghelper:any = new googleHelper();
        switch (platform) {
            case "window":
                fn = wndhelper[fnctname];
                break;
            case "office":
               
                fn = offhelper[fnctname];
                console.log(fn); //return undefined
                console.log(fnctname);
                break;
            case "google":
                fn = gghelper[fnctname];
                break;
            default:
                break;
        }
        // is object a function?
        if (typeof fn === "function") fn.apply(null, fnctparams);
    }
}

OfficeHelper.tsx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    export class officeHelper {
  constructor() { }
static GetEmail = () => {
    return Office.context.mailbox.userProfile.emailAddress;
  }
}

login.tsx

 let userEmailAddress = platformHelper.callFCT("GetEmail"); 
 console.log(userEmailAddress ) // UNDEFINED

The fn function is always undefined and the email address is not being returned as GetEmail is not being called

>Solution :

In your code, GetEmail is a static function of officeHelper class, so you have to access it through officeHelper.GetEmail (or officeHelper["GetEmail"]), instead of new officeHelper().GetEmail.

Then, as pointed out in the question comments and other answers, do not forget to return the result of fn.apply.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading