【问题标题】:processing http call's response: filter is not a function处理 http 调用的响应:过滤器不是函数
【发布时间】:2018-09-09 04:33:54
【问题描述】:

提供者代码:

import {AutoCompleteService} from 'ionic2-auto-complete';
import { Http } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
@Injectable()
export class AutoSuggestProvider implements AutoCompleteService {

  constructor(public http: HttpClient, public HTTP: Http) {
    console.log('Hello AutoSuggestProvider Provider');
  }
  getResults(keyword:string) {
    return this.HTTP.get("https://restcountries.eu/rest/v1/name/"+keyword) .map( result => { return result.filter(item => item.name.toLowerCase().startsWith(keyword.toLowerCase()) ) });
  }

}

问题线:

返回 this.HTTP.get("https://restcountries.eu/rest/v1/name/"+keyword) .map( 结果 => { 返回结果.filter(item => item.name.toLowerCase().startsWith(keyword.toLowerCase()) ) });

正如你所看到的,这是一条有问题的行。

“响应”类型上不存在属性“过滤器”。

请帮助我如何解决这个问题?

【问题讨论】:

  • import { Pipe, PipeTransform } from '@angular/core';
  • 尝试添加这个 'import 'rxjs/add/operator/filter';
  • 同时检查你的回复是否为空

标签: javascript angular ionic2


【解决方案1】:

请试试这个

import { Http } from '@angular/http'
import 'rxjs/add/operator/map';

getResults(keyword:string){
return this.Http.get("https://restcountries.eu/rest/v1/name/"+keyword).map(res=>{
  return res.json()
}).map(fil=>{
    return fil.filter(res=>{
      return res.name.toLowerCase().startsWith(keyword.toLowerCase())
    })
  })

}

在第一个地图中以 json 形式返回,在第二个地图中执行过滤功能以根据关键字过滤结果

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2022-07-16
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多