Reputation: 4881
I have a typescript class that contains the following
getOrgList(oo: fhir.Organization) {
var olist: orgRoles[] = [];
var filtered = oo.extension.filter(this.getRoleExt);
filtered.forEach(function (value) {
var org = new orgRoles();
value.extension.forEach(function (innerValue) {
switch (innerValue.url) {
case 'role':
org.roleName = innerValue.valueCoding.display;
break;
case 'primaryRole':
org.primaryRole = innerValue.valueBoolean;
break;
case 'activePeriod':
var periodType = innerValue.valuePeriod.extension[0].valueString;
var periodExt = innerValue.valuePeriod;
var periodDisplay= this.getPeriodDisplay(periodExt);
break;
case 'status':
org.activeStatus = innerValue.valueString;
break;
}
});
olist.push(org);
});
return olist;
}
This is currently failing on the line
var periodDisplay= this.getPeriodDisplay(periodExt);
With an error
ERROR TypeError: Cannot read property 'getPeriodDisplay' of undefined
However, getPeriodDisplay is defined as the next method in the same class.
getPeriodDisplay(pp) {
return "++++";
}
I have other code that calls getPeriodDisplay() from outside the class, but calling it from a different method within the class seems to cause the problem
Any ideas on how I should address this?
Upvotes: 0
Views: 1597
Reputation: 2822
You need to switch regular functions for arrow functions to have access to correct this
(class instance):
getOrgList(oo: fhir.Organization) {
var olist: orgRoles[] = [];
var filtered = oo.extension.filter(this.getRoleExt);
filtered.forEach((value) => {
var org = new orgRoles();
value.extension.forEach((innerValue) => {
switch (innerValue.url) {
case 'role':
org.roleName = innerValue.valueCoding.display;
break;
case 'primaryRole':
org.primaryRole = innerValue.valueBoolean;
break;
case 'activePeriod':
var periodType = innerValue.valuePeriod.extension[0].valueString;
var periodExt = innerValue.valuePeriod;
var periodDisplay= this.getPeriodDisplay(periodExt);
break;
case 'status':
org.activeStatus = innerValue.valueString;
break;
}
});
olist.push(org);
});
return olist;
}
Upvotes: 1