skip to Main Content

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

3

Answers


  1. callFCT doesnt return value.
    Add return in callFCT

    
     // ...
         if (typeof fn === "function") {
            return fn(fnctparams)
         };
     // ...
    
    Login or Signup to reply.
  2. 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
    }
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search