title: Angular subscribe to data returned from API
body: <p>I'm trying to clean up my Ionic app and move redundant functions into a provider. Below is an example of what most of my API functions look like (except I've stripped out a lot of irrelevant stuff for brevity) and they are working perfectly.</p>
  
  <pre class="lang-js prettyprint-override"><code>getDataFromApi() {
      ....
      Promise.all([
          ...
      ]).then((result) =&gt; {
          let headers = new Headers();
          ...
          let body = new FormData();
          ...
          this.http.post(url, body, headers)
              .map(res =&gt; res.json())
              .subscribe(data =&gt; {
                  if (data == '{}') {
                      this.mydata = [];
                  } else {
                      this.mydata = data;
                  }
              }, error =&gt; { });
      });
  }
  </code></pre>
  
  <p>So what I've done is move the function into the provider and changed it like so</p>
  
  <pre class="lang-js prettyprint-override"><code>getDataFromApi() {
      ....
      Promise.all([
          ...
      ]).then((result) =&gt; {
          let headers = new Headers();
          ...
          let body = new FormData();
          ...
          return this.http.post(url, body, headers));
      });
  }
  </code></pre>
  
  <p>And then inside the page's constructor I'm calling it like this</p>
  
  <pre class="lang-js prettyprint-override"><code>this.service.getDataFromApi()
      .map(res =&gt; res.json())
      .subscribe(data =&gt; {
          if (data == '{}') {
              this.mydata = [];
          } else {
              this.mydata = data;
          }
      }, error =&gt; { });
  </code></pre>
  
  <p>Obviously that's not working. I've been pouring over SO posts for 2 days now and can't get other people's examples and answers to work properly either. I've tried mapping in the provider and subscribing when calling it in the page, but no matter what I try just keep receiving errors. I know this function is returning data because I can see it before moving it into the provider.</p>
  
  <p>What am I doing wrong?</p>