Angular Http获得404停止进一步调用

时间:2018-05-26 19:04:07

标签: angular http-status-code-404

这是黄铜钉。我有这个服务电话

this.searchTerm.valueChanges.debounceTime(400)
    .filter(searchTerm => { return searchTerm.length >= 3;})
    .distinctUntilChanged()
    .switchMap((searchTerm) => this._scryfallService.getCards(searchTerm))
    .subscribe(data => this.cardSearchResults = data.data,
                err => this.cardSearchResults = [];
    );

它的背面看起来像这样

getCards(term: string): Observable<any> {
    let url = this.scryFallEndpoint + 'search?q="' + encodeURI(term) + '"+unique:prints+not:digital' ;
    return this._http.get(url)
    .map(res => res);
}

如果使用您的搜索字词找不到内容,我拨打电话的服务将返回404。如果我收到404错误,搜索将完全停止工作,我必须刷新页面才能进行另一次搜索。如果搜索成功,我可以在我的搜索框中重新输入,然后再拨打另一个电话。

摘要:当服务调用404s时,它似乎阻止了任何其他调用

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

你应该捕获scryfallService抛出的任何错误,并返回一个可以进一步处理的值。否则,观察者将完成。

this.searchTerm.valueChanges.debounceTime(400)
    .filter(searchTerm => { return searchTerm.length >= 3;})
    .distinctUntilChanged()
    .switchMap((searchTerm) => this._scryfallService.getCards(searchTerm).catch(err => Observable.of('error happened but please continue')))
    .subscribe(data => this.cardSearchResults = data.data,
                err => this.cardSearchResults = [];
    );