I'm quite new to programming. I have spend last 3 days trying to figure this out. So any help really appreciate it.
I have access to clientID inside a for loop. (Please look at the last line in the code below). There is only one ClientID which I'm successfully able to print.
I want this client ID to be exported from this module so that I can use it in other modules.
import { Configurations } from './models';
type MethodNames = 'init' | 'event';
export const DEFAULT_NAME = '_hw';
interface LoaderObject {
q: Array<[MethodNames, {}]>;
}
export default (
win: Window,
defaultConfig: Configurations,
scriptElement: Element | null,
render: (element: HTMLElement, config: Configurations) => void
) => {
const instanceName =
scriptElement?.attributes.getNamedItem('id')?.value ?? DEFAULT_NAME;
const loaderObject: LoaderObject = win[instanceName];
if (!loaderObject || !loaderObject.q) {
throw new Error(
`Widget didn't find LoaderObject`);
}
if (win[`loaded-${instanceName}`]) {
throw new Error(
`Widget already loaded`)
);
}
let targetElement: HTMLElement;
for (let i = 0; i < loaderObject.q.length; i++) {
const item = loaderObject.q[i];
const methodName = item[0];
console.log(methodName);
if (i === 0 && methodName !== 'init') {
throw new Error(
`Failed to start Widget`);
} else if (i !== 0 && methodName === 'init') {
continue;
}
const valueObject = Object.assign(defaultConfig, item[1]);
const clientID = valueObject.clientID
console.log("ClientID", clientID)
}
//rest of the code....
I have also tried this. defined a variable clientID outside the for loop and then storing value from inside the for loop. But on printing, I'm getting undefined
var clientID;
console.log("....last Client ID", clientID)
const valueObject = Object.assign(defaultConfig, item[1]);
clientID = valueObject.clientID