错误TypeError:subscribe不是函数

时间:2018-04-19 12:36:59

标签: javascript angular angular4-httpclient

我试图从基于某个元素的数组中获取值,然后获取完全匹配的元素。但不知何故,我在这个函数getProduct(name1:string)面临这个错误,我已经导入了地图。将非常感谢帮助。 this.searchProduct(...)。subscribe不是函数

 products: Product[] = [];
 product: Product;

 searchProduct(name2: string): Observable<Product[]> {
    var config = {
        headers: {
            'Content-Type': 'application/json'
        }
    };
    const ans = name2;
    let obj = { name_product: ans }
    let body = JSON.stringify(obj);
    let header = new HttpHeaders();
    header = header.append('Content-Type','application/json');
    return this.httpClient.post('postRestService', body, config).subscribe(res => {
        this.products = res;
        console.log(this.products);
    })
 }

  getProduct(name1: string): Observable<Product> {
   return this.searchProduct(name1).subscribe(products => 
   products.find(product => product.name1 == name1));
 }

1 个答案:

答案 0 :(得分:1)

http服务从角度2变为角度4.在角度4中,您不需要地图:

products: Product[] = [];
 product: Product;

 searchProduct(name2: string): Observable<Product[]> {
    var config = {
        headers: {
            'Content-Type': 'application/json'
        }
    };
    const ans = name2;
    let obj = { name_product: ans }
    let body = JSON.stringify(obj);
    let header = new HttpHeaders();
    header = header.append('Content-Type','application/json');
    this.http.post('postRestService', body, config).subscribe(res => {
        this.products = res;
        console.log(this.products);
    })
 }

  getProduct(name1: string): Observable<Product> {
   return this.searchProduct(name1).subscribe(products => products.find(product => product.name1 == name1));
 }