I'm struggling with the correct terminology for this issue, so I apologize if my title is not accurate. When attempting to utilize 'this' within a method, I am encountering an issue where it returns as 'undefined' during execution of the code.
My goal is to iterate through each 'api' and invoke it, allowing me to then manipulate the outcome. Each API may have distinct parameter data.
enum APIPermissions{ HIGH, LOW };
class Api{
baseurl: string;
constructor(admin: boolean){
if(admin){
this.baseurl = 'localhost';
}else{
this.baseurl = 'admin.localhost'
}
}
api1(param1: string){
return this.baseurl+'/api1 data';
}
api2(param1: string, param2: string){
return this.baseurl+'/api2 data';
}
api3(json: any){
return this.baseurl+'/api3 data';
}
}
const api = new Api(true);
const data = [
{
func: api.api1,
params: ['test1'],
permissions: [APIPermissions.LOW]
},
{
func: api.api2,
params: ['test1', 'test2'],
permissions: [APIPermissions.HIGH]
}
]
for(const d of data){
const result = d.func('a','b'); // d.params doesn't work
console.log(result);
// Check result and do something
}
The current output is: "undefined/api1 data" "undefined/api2 data"
However, I anticipate receiving "admin.localhost/api1 data". I reviewed the Typescript documentation but found the provided example to be confusing. Additionally, I am unsure how to correctly handle the part of d.func() that requires 2 parameters. Any assistance on resolving these issues would be greatly appreciated.