I want to call a NodeJS class using only a Class name and a method name. How do I do this if I only have the string value of the class and method? Assuming I have this controller
export default class PageController extends BaseController {
async mainMethod(request, response) {
const mapClass = {
class: "MyClass",
method: "testMethod",
};
global.${mapClass.class}.${mapClass.method}(); <-- How do I call the class MyClass.testMethod();
}
}
Now in my MyClass.js I have this function
// Inside MyClass.js
export default class MyClass {
testMethod(params) {
return "My response here";
}
}
UPDATE:
import resolver from '../services/Resolver';
export default class PageController extends BaseController {
async mainMethod(request, response) {
const mapClass = {
class: "MyClass",
method: "testMethod",
};
let classInstance = new resolver[mapClass.class];
classInstance[mapClass.method]();
}
}
// Inside ../services/Resolver.js
import MyClass from './MyClass';
export {
MyClass,
}
// Content of MyClass.js
// Inside MyClass.js
export default class MyClass {
testMethod(params) {
return "My response here";
}
}
Call just hangs when i reached the let classInstance = new resolver[mapClass.class];
and just gives me a request timeout