gpt4 book ai didi

javascript - 处理 http 调用的响应 : filter is not a function

转载 作者:太空狗 更新时间:2023-10-29 19:33:23 24 4
gpt4 key购买 nike

提供商代码:

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()) ) });
}

}

有问题的行:

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

如您所见,这是一条有问题的行。

Property 'filter' does not exist on type 'Response'.

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

最佳答案

请试试这个

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())
})
})

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

关于javascript - 处理 http 调用的响应 : filter is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49569260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com