Calling a provider function returns undefined

Ionic version: (check one with “x”)
(For Ionic 1.x issues, please use https://github.com/ionic-team/ionic-v1)
[ ] 2.x
[x] 3.x
[ ] 4.x

I’m submitting a … (check one with “x”)
[x] bug report
[ ] feature request

Please do not submit support requests or “How to” questions here. Instead, please use one of these channels: https://forum.ionicframework.com/ or http://ionicworldwide.herokuapp.com/

Current behavior:

My function calls my REST API service provider to get data from my server. When I display data inside the provider with a console.log() I saw it but when I display the data in my original calling function (the data is passed with a Promise) it’s undefined.

Expected behavior:

The data should be displayed in both functions.

Related code:

Here is the method of my provider:

getBusinesses(token: string){
    return new Promise((resolve,reject) => {
      let request = new HttpRequest('GET',this.listBusinesses,{
        headers: new HttpHeaders({
          'X-AUTH-TOKEN': token
        })
      });

      this.http.request(request).subscribe(data => {
        console.log(data['body']); //works, show my JSON array
        resolve(data);
      }, err => {
        reject(err);
      })
    });
  }

And here is the method of my page function:

private getBusinesses() {
    this.cityfidAPI.getBusinesses(token).then(res => {
      this.businesses = res['body'];
      console.log(this.businesses); //undefined
    });
}

Other information:

Ionic info: (run ionic info from a terminal/cmd prompt and paste output below):

cli packages: (C:\Users\cypou\AppData\Roaming\npm\node_modules)

    @ionic/cli-utils  : 1.19.0
    ionic (Ionic CLI) : 3.19.0

global packages:

    cordova (Cordova CLI) : 7.1.0

local packages:

    @ionic/app-scripts : 3.0.1
    Cordova Platforms  : none
    Ionic Framework    : ionic-angular 3.8.0

System:

    Android SDK Tools : 26.1.1
    Node              : v8.6.0
    npm               : 5.5.1
    OS                : Windows 10

Environment Variables:

    ANDROID_HOME : C:\Users\cypou\AppData\Local\Android\Sdk

Misc:

    backend : pro

If I ask that kind of question here it’s because I did not get any answer on Ionic Forums or on the Slack channel

Author: Fantashit

1 thought on “Calling a provider function returns undefined

  1. @Cypaubr – It is highly unlikely that this is an issue within Ionic itself since all of the code you share above is Angular related and not associated with anything provided by the Ionic Framework. This forum is only for Ionic bugs and feature requests, so I am going to close this.

    I am willing to take a look at your code, but I can’t help debug code I cannot see. This sample does not have to be your actual project code but could be a sample application that you put together using a REST API that you toss together for the demonstration purpose.

Comments are closed.