I was trying to call a third party api and it seems to be working fine in the class
Following is my provider class
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the EventService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class EventService {
public data: any;
constructor(public http: Http) {
console.log('Hello EventService Provider');
}
loadEvents() {
if (this.data) {
// already loaded data
return Promise.resolve(this.data);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular HTTP provider to request the data,
// then on the response, it'll map the JSON data to a parsed JS object.
// Next, we process the data and resolve the promise with the new data.
this.http.get('/api/events')
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
resolve(this.data);
});
});
}
loadEventDetail(eventId){
if (this.data) {
return Promise.resolve(this.data);
}
return new Promise(resolve => {
this.http.get('/api' + '/events/' + eventId )
.map(res => res.json())
.subscribe(data => {
this.data = data;
console.log(data);
resolve(this.data);
});
});
}
}
In my Page module class i have called this service for getting the service and was able to view the same in console log
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {EventService} from '../../providers/event-service';
/**
* Generated class for the EventDetail page.
*
* See http://ionicframework.com/docs/components/#navigation for more info
* on Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-event-detail',
templateUrl: 'event-detail.html',
providers: [EventService]
})
export class EventDetailPage {
public eventId: any;
public event: any;
constructor(public navCtrl: NavController, public navParams: NavParams, public eventService: EventService) {
this.eventId = navParams.get('eventId');
this.loadEventDetails(this.eventId);
}
loadEventDetails(eventId){
this.eventService.loadEventDetail(eventId)
.then(data => {
this.event = data;
console.log(this.event.title);
});
}
}
But when i am trying to acces the event object in my html file like this
<ion-header>
<ion-navbar color="danger">
<ion-title>Event Details</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<p>{{ eventi.title}}</p>
</ion-content>
I am getting an error saying ,
Runtime Error
Uncaught (in promise): TypeError: Cannot read property ‘title’ of undefined
THis is the json response
{"id":2,"title":"Meetup - Ruby On Rails with React.js","description":"A meetup will be held at technopark Thejaswini building , organised by Weavelabs","date":"2017-05-25T06:05:34.401Z","event_type":"FREE","street":"Kazhakootam","city":"Trivandrum","state":"Kerala","country":"India","latitude":1.094775,"longitude":-1.09878378,"contact_details":[{"id":2,"primary_number":"9562141330","email":"manu@yahoo.in","secondary_number":"978645321"}],"photos":[],"category":{"id":2,"name":"Technical"}}
Another api listing events is working fine and i am not sure whether i am doing anything wrong.Tried almost every possible way but seems to be not working.
I just moved to Ionic 3
ANy help will be appreciated


