When working with typescript/javascript, I encountered an issue while trying to fetch the 'statute' from a data object:
{_id: "31ad2", x: 21.29, y: -157.81, law: "290-11",....}
I attempted to assign data.law to a variable, but received a typeerror stating that it cannot read property 'law' of undefined?
Strangely, when I console log 'data.law' at line 11 or result[0] at line 18, the correct value is displayed...
sectionsSuccess(res: Response) {
this.allSections = [];
this.sections = [];
this.loadingSections = false;
try {
let jsonRes = res.json();
this.jsonResLength = jsonRes.length;
for (var a = 0; a < this.jsonResLength; a++) {
let js = jsonRes[a];
js.bookmarked = this.server.isInBookmark(js);
this.allSections.push(js);
if (a < 15) {
this.sections[a] = this.allSections[a];
}
}
} catch (e) {
alert("Exception: " + e.message);
}
for (var i = 0; i < this.allSections.length; i++) {
this.allSections[i] = this.convertLocationDataToStatutes(this.allSections[i]);
}
// complete code added above
for (var i = 0; i < this.sections.length; i++) {
this.sections[i] = this.convertLocationDataToStatutes(this.sections[i]);
}
}
convertLocationDataToStatutes(data: any): any {
var self = this;
var chapterandsection = data.law; //line 11
var values = chapterandsection.split('-');
var chapter = values[0];
var section = values[1];
(self.server).getSection(chapter, section)
.map(response => response.json()).subscribe(result => {
return result[0]; // line 18
});
}