title: Angular 2 Cannot read property 'subscribe' of undefined TypeError: Cannot read property 'subscribe' of undefined
body: <p>I have tried many propositions on fixing this error here on stackoverflow, but now I just need to ask as I have spent many hours with literally getting nowhere now.</p>
  
  <p>I have this simple Service: </p>
  
  <pre><code>constructor(private http: Http, private tokenResolver: TokenResolver) {}
  
  public getEventHistory(): Observable&lt;heatmapElement[]&gt; {
  
    this.tokenResolver.getToken(true).subscribe(token =&gt; {
      var headers = new Headers();
      headers.append('Authorization', 'Bearer ' + token);
      headers.append('Content-Type', 'application/json');
  
      this.result = this.http.get(this.restApi, new RequestOptions({headers: headers}))
          .map((res: Response) =&gt; res.json());
    });
  
    return this.result as Observable&lt;heatmapElement[]&gt;;
  }
  </code></pre>
  
  <p>I want to get the data using : </p>
  
  <pre><code>public demoData: heatmapElement[] =[];
  
  getEventHistory(): void {
    this.eventHistoryService.getEventHistory()
                            .subscribe(data =&gt; this.demoData = data,);
  }
  </code></pre>
  
  <p>This creates a error that I just cant seem to fix:</p>
  
  <blockquote>
    <p>EXCEPTION: Uncaught (in promise): Error: Error in <a href="http://localhost:5555/app/dashboard/dashboard.html:13:8" rel="noreferrer">http://localhost:5555/app/dashboard/dashboard.html:13:8</a> caused by: Cannot read property 'subscribe' of undefined
        TypeError: Cannot read property 'subscribe' of undefined</p>
  </blockquote>
  
  <p>I would be very grateful for help, 
  thank you</p>