我对服务订阅了我从外部API请求图像URL并将其存储在共享服务中。
逻辑:
1.清空当前图像数组
2.请求来自API的图像,然后将其推到(新清除)数组(响应=>)
3.如果URL返回错误,请用默认值(占位符)图像(错误=>)
出于某种原因,尽管有一个错误,但并未处理错误。我已经发布了上下文的组件和错误消息。
component.ts
getImages(vehicleID: number, color: string): void {
this._carConfigService.car.colors.exterior.urls = [];
this._FAPIService.getImagesForColors(vehicleID, color)
.subscribe(
response => {
response.products[0].productFormats.forEach(pF => {
this._carConfigService.car.colors.exterior.urls.push(pF.assets[0].url);
});
},
error => {
this.errorMessage = <any>error;
if (this._carConfigService.car.colors.exterior.urls.length === 0) {
this._carConfigService.car.colors.exterior.urls = this._carConfigService.defaultImages;
}
});
}
我收到以下错误:
Failed to load resource: the server responded with a status of 422 ()
EXCEPTION: Response with status: 0 for URL: null
Uncaught Response
您需要使用例外处理的常规方法来处理它们
export class TaskService {
private _url = "api/products/datanew.json";
constructor(private _http: Http) {
}
getTasks(): Observable<any[]> {
return this._http.get(this._url)
.map((response: Response) => <any[]>response.json())
.do(data => console.log("data we got is " + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error: Response) {
console.log(error);
return Observable.throw(error.json().error || 'Internal Server error');
}
}
另外,我有一个问题。
- 您正在获取数据,但您没有分配任何对象?无返回类型