Promise() returning "Undefined"

Hi guys :slight_smile:

Working on my first project in Ionic more or less following this tutorial.
In general trying to call the REST Api, with prayer types , and present it on the screen.

The issue is that, when calling loadPrayerType() from the constrcutor (or on click the button), method prayerProvider.load() return “undevined” value. While inside itselfs it has data (retrieve from API, log, etc). Cheked it on DebugTool on Chrome.

Any suggestion?

BTW I’m newbie on Ionic and JavaSript in general - what probably is a reason :-).

File wspolnota.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {PrayerProvider} from '../../providers/prayer/prayer';
import { HttpModule } from '@angular/http';

@Component({
  selector: 'page-wsp-lnota',
  templateUrl: 'wsp-lnota.html',
  providers: [PrayerProvider]
})

export class WspLnotaPage {
  public prayer_type: any;

  constructor(public navCtrl: NavController, public prayerProvider: PrayerProvider) {
        
    this.loadPrayerType();
  }
  
 
 loadPrayerType(){
       
    this.prayerProvider.load().then(data => {
      this.prayer_type = data.results;
    });
     }
}

File prayer.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class PrayerProvider {
  data: any;
  
  constructor(public http: Http) {
    console.log('Hello PrayerProvider Provider');
    this.data = null;
  }
 
  
  load() {
   debugger;
    if (this.data) {
      console.log("Mam już dane "+ this.data);
      // already loaded data
      return Promise.resolve(this.data);
    }
       
   // don't have the data yet
    return new Promise(resolve => {
 
      this.http.get('http://localhost:8000/book/')
        .map(res => res.json())
        .subscribe( data => {
          this.data = data;
          resolve(this.data);
        });
      });
     }
  }

I think the code quality in that blog post is abysmal and you should just nuke it from orbit and start over with other sources. I would recommend part 6 of the Tour of Heroes; others might have other suggestions.

Solved.
Thanks for your advice. Based on it I started worling with this new tutorial …rewrote lots of and stuck in similar place with similar error :-). But keep fighting and found a bag in converting http.get response into Ionic obiects.

BTW
A first glance I found your respons as go fly a kite… but decided to follow it… and finally that found solution. Thanks