0

how can I get the content base on the id ?

how can I get the content when the id is 1 for example localhost:5555/product/1 ? my route file is already setup

I have a json file like

[
  {
    "id": "0",
    "name": "xxxx",
    "icon": "https://wikidownload.com/Download/xxxx.png",
    "Website":"http.xxx.com"
  },
  {
    "id": "1",
    "name": "zzz",
    "icon": "zzzz.png",
    "Website":"http.zzz.com"
  },
  {
    "id": "2",
    "name": "yyy",
    "icon": "yyy.png",
    "Website":"http.yyy.com"
  }
]

in my component :

  ngOnInit() {
    this.route.params
      .subscribe(
         (params : Params) => {
           this.id = params.id; //get the id from the route
           this.getWhipById(this.id);
         }
       )
  }

  getWhipById(id:number){
    console.log ( id );
    // console.log ( this.productService.getById(id) );

    // this.productService.get().map(
    //   res => {
    //     return res.filter(item => item.id === id)[0];
    //   });
  }

product.service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';


@Injectable()
export class ProductService {


  constructor(private http: Http) {}



  /**
   * Returns an Observable for the HTTP GET request for the JSON resource.
   * @return {string[]} The Observable for the HTTP request.
   */
  get(): Observable<string[]> {
    return this.http.get('assets/data/products.json')
      .map((res: Response) => res.json())
      //              .do(data => console.log('server data:', data))  // debug
      .catch(this.handleError);
  }

  getById(id: number): Observable<string[]> {
    return this.get()
      // 
      // .map((res: Response) => res.find( res => res.id == id));
      // .map(movies => movies.find(movie => movie.id == id));
  }
  /**
   * Handle HTTP error
   */
  private handleError (error: any) {
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }
}

I'm passing the id to the getWhipById() in this point is where I get confuse the ProductService.get() will get all content ..I used map filter , map find but I could get the content just for the id that I'm passing. Another thing is to debug ... if I do console.log() it only shows observable and other properties but not anything about the json content

Marcogomesr
  • 2,614
  • 3
  • 30
  • 41

2 Answers2

0

An observable only runs if it is subscribed to, so if it returns a .map, but nothing subscribes to it, it will never run the .map. Try subscribing to the result of the map

getWhipById(id:number){
         let mapped = this.productService.get().map(
           res => {
             return res.filter(item => item.id === id);
           });
         mapped.subscribe((response) => {
          console.log('subscribe' + response.json());
        });        
      }
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
0

need to call res.json() before call filter()??

this.ProductService
  .get()
  .map(res => res.json().filter(item => item.id === id))
  .subscribe(resp=>console.log(resp));