title: How to fix ‘Cannot read property 'forEach' of undefined’ error in Angular
body: <p>I have a bug, but I don't know what else to do to fix it. Below is my code and the error:</p>
  
  <pre><code>courses: any[] = [];
  
  ngOnInit() {
      this.locale = this.localeService.currentLocale();
      this.user = this.authService.getCurrentUser();
      this.userService.getCoursesInProgress(this.user.id).subscribe(courses =&gt; {
          this.coursesInProgress = [];
          courses.forEach(n =&gt; {
              const courseObj: Course = new Course(n);
              this.coursesInProgress.push(courseObj);
          });
      });
      this.userService.getCoursesFinished(this.user.id).subscribe(courses =&gt; {
          this.coursesFinished = [];
          courses.forEach(n =&gt; {
              const courseObj: Course = new Course(n);
              this.coursesFinished.push(courseObj);
          });
      });
  }
  </code></pre>
  
  <blockquote>
    <p>TypeError · Cannot read property 'forEach' of undefined</p>
  </blockquote>