1

I keep getting this message in the console when I try and run the below code: "undefined is not an object (evaluating 'this.courseAvailThisTerm')". The courseAvailThisTerm() function works properly because it works in other contexts. Any one have an idea why I keep getting this error message? I've tried changing the for loop version but that doesn't work either.

for(let quarter of degreePlan) {
    foundationCourseSet.forEach(function(fc) {
            if(this.courseAvailThisTerm(fc, quarter.term))
                console.log("It was avail!");
        });
}
beh1
  • 139
  • 1
  • 2
  • 15
  • 1
    Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – CRice May 15 '18 at 18:41

1 Answers1

1

Try this:

for(let quarter of degreePlan) {
    foundationCourseSet.forEach((fc) => {
            if(this.courseAvailThisTerm(fc, quarter.term))
                console.log("It was avail!");
        });
}

I change function (fc) {..} to arrow function (fc) => {...} which preserve this in context on function execution.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345