Hey Ionic community,
I just discovered Ionic and I’m trying to build a very simple app that gets json data from a server, then displays it as cards with a tap on one of the cards opening a detail view of the item.
My problem is that I get the error EXCEPTION: TypeError: undefined is not an object (evaluating 'l_gameDetails1.gameName') in [{{ gameDetails.gameName }} in DetailsPage@8:25]
and that my page displays no content.
The involved files look like this:
details.ts:
import {Page,NavController, NavParams} from 'ionic-angular';
import {OnInit} from 'angular2/core';
import {GameService} from "../../../services/game-service";
@Page({
templateUrl: 'build/pages/details/details.html',
providers: [GameService]
})
export class DetailsPage implements OnInit{
public game;
public gameDetails;
constructor (private _github: GameService, private _nav: NavController, private _navParams: NavParams){
this.game = _navParams.get('game');
}
ngOnInit(){
this._github.getDetails(this.game).subscribe(
data => this.gameDetails = data.json(),
err => console.error(err),
() => console.log('getDetails completed')
);
}
}
details.html:
<ion-navbar *navbar>
<ion-title>
{{ game.gameName }}
</ion-title>
</ion-navbar>
<ion-content>
<ion-card>
<ion-card-header>
{{ gameDetails.gameName }}
</ion-card-header>
</ion-card>
</ion-content>
game-service.ts:
import {Injectable} from 'angular2/core';
import {Http, Headers} from 'angular2/http';
@Injectable()
export class GameService {
constructor(private http: Http) {
}
getGames() {
let games = this.http.get(`http://some.api.url`);
return games;
}
getDetails(game){
let detail = this.http.get(`http://some.api.url`);
return detail;
}
}
The API’s json response looks like this:
{"gameName":"Stardew Valley"}
I tried to debug the error and found out that when I set the value of gameDetails
in details.ts
manually (e.g. public gameDetails = {gameName: "test"};
) and leave the ngOnInit
method blank everything seems to work fine.
My best (not very educated) guess is that there is some kind of timing problem due to the JSON not loading as fast as the page.
What can I do to fix my problem?
Thanks in advance for the help.