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
callFCT doesnt return value.
Add return in callFCT
You forget to return the content from the executed method. You should do:
In your code,
GetEmail
is astatic
function ofofficeHelper
class, so you have to access it throughofficeHelper.GetEmail
(orofficeHelper["GetEmail"]
), instead ofnew officeHelper().GetEmail
.Then, as pointed out in the question comments and other answers, do not forget to return the result of
fn.apply
.