5

I'm trying to get data from a JSON file following the http tutorial from the Angular2 documentation:

Service:

import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Hero } from './hero';

private heroesUrl = 'app/heroes';  // URL to web api

constructor(private http: Http) { }

getHeroes(): Promise<Hero[]> {
    return this.http.get(this.heroesUrl)
        .toPromise()
        .then(response => response.json().data as Hero[])
        .catch(this.handleError);
}

Component:

import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from './hero.service';

@Component({
  selector: 'my-app',
  template: // some template,
  styles: // some style,
  providers: [HeroService]
})

export class AppComponent implements OnInit {
  title = 'Tour of Heroes';
  heroes: Hero[];

  constructor(private heroService: HeroService) { }

  getHeroes(): void {
    this.heroService.getHeroes().then(heroes => {
        this.heroes = heroes;
        console.log('heroes', this.heroes);
    });
  }

  ngOnInit(): void {
    this.getHeroes();
  }
}

Model:

export class Hero {
  constructor(
    public id: number,
    public name: string,
    public power: string,
    public alterEgo?: string
  ) {  }
}

I replaced private heroesUrl = 'app/heroes'; with my endpoint that is a JSON file (private heroesUrl = 'app/mockups/heroes.json';), that contains data according to hero model.

But when i run my app i get no data and there is any error message. Isn't my code correct?

19
  • Where do you not get data? Can you please add a console.log(data); in the code in your question where you expect data (or whatever name) you expect to have a value? Commented Sep 5, 2016 at 9:20
  • Where can i add console.log(data)? Commented Sep 5, 2016 at 9:29
  • I don't understand the question. Where do you expect what variable to have a value? To debug it you can add a console.log(...) line. This allows you to investigate yourself and allows us to see what you expect. I can't answer your question when I don't know what you expect to happen. Commented Sep 5, 2016 at 9:33
  • I expect to have data in heroes variable. How to add console.log(data) in getHeroes() function? Commented Sep 5, 2016 at 9:49
  • 6
    I solved. The problem was that the JSON object must start with a property that has the same name of response.json().data. So i renamed the first property to data. Please upvote this comment so other members can see it. Commented Sep 5, 2016 at 12:09

1 Answer 1

3

The solution is that the JSON object must start with a property that has the same name of response.json().data.

Renaming the first property to data does the job.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.