0

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

    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

Sora
  • 2,465
  • 18
  • 73
  • 146
  • 2
    the `callFCT("GetEmail")` run the function which doesn't have return so you have undefined. You forget return – Elikill58 Feb 17 '23 at 10:21
  • fn.apply should call GetEmail and GetEmail should return the email with > return Office.context.mailbox.userProfile.emailAddress; – Sora Feb 17 '23 at 10:22
  • 1
    yes, it return the value in the `if`, but this same result isn't return in callFCT – Elikill58 Feb 17 '23 at 10:24

3 Answers3

1

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.

ghybs
  • 47,565
  • 6
  • 74
  • 99
  • noted but i am getting the following error: Element implicitly has an any type because expression of type any can't be used to index type typeof officeHelper – Sora Feb 17 '23 at 10:32
  • See e.g. https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b – ghybs Feb 17 '23 at 10:39
0

callFCT doesnt return value. Add return in callFCT


 // ...
     if (typeof fn === "function") {
        return fn(fnctparams)
     };
 // ...
rycha
  • 679
  • 2
  • 9
0

You forget to return the content from the executed method. You should do:

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;
    }
    if (typeof fn === "function")
       return fn.apply(null, fnctparams); // return the result of GetEmail
    return null; // no function -> return nothing
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • i understand but in my case `if (typeof fn === "function")` should return true but instead it is returning undefined so callFct willl always return null, i am not sure why – Sora Feb 17 '23 at 10:26