angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 231 forks source link

web calls always return an array #132

Closed TrentonAdams closed 7 years ago

TrentonAdams commented 7 years ago

The issue I submitted appears to have vanished. Very strange.

When I create a data service with createDb like this...

  createDb() {
    const heroes = [
      { id: 0,  name: 'Zero' },
      { id: 11, name: 'Mr. Nice' },
      { id: 12, name: 'Narco' },
      { id: 13, name: 'Bombasto' },
      { id: 14, name: 'Celeritas' },
      { id: 15, name: 'Magneta' },
      { id: 16, name: 'RubberMan' },
      { id: 17, name: 'Dynama' },
      { id: 18, name: 'Dr IQ' },
      { id: 19, name: 'Magma' },
      { id: 20, name: 'Tornado' }
    ];
    return {heroes};
  }

And I make a hero service like the following...

@Injectable()
export class HeroService
{
  private heroesUrl = 'api/heroes';

  constructor(private http: Http)
  {
  }

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

  private handleError(error: any): Promise<any>
  {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

  getHero(id: number): Promise<Hero>
  {
    const url = `${this.heroesUrl}/${id}`;
    console.log('angular log');

    return this.http.get(this.heroesUrl)
      .toPromise()
      .then(response => response.json().data as Hero)
      .catch(this.handleError);
  }

}

Both getHero(id) and getHeroes() return the exact same result, namely an array of heroes.

Am I doing it wrong, or is this a bug?

Kandium commented 7 years ago

But shouldn't you be passing in "url" to the getHero this.http.get function instead of "this.heroesUrl"?

wardbell commented 7 years ago

@Kandium is exactly right. Closing.