2016-05-26 29 views
7

İyi çalışıyor Http hizmeti ile bir ajax araması yapmaya çalışıyorum.yakalamak, http.get için çalışmıyor Angular2

Artık hata durumlarını ele almak istiyorum. Örnek 404 veya başka bir hata alırsak.

Aşağıdaki kodu kullanıyorum.

import {Component,Inject} from '@angular/core'; 
import {Http, Response,HTTP_PROVIDERS} from '@angular/http'; 
import {DoService} from './service'; 
import 'rxjs/Rx'; 
import 'rxjs/add/operator/catch'; 

@Component({ 
    selector: 'comp-one', 
    template: `<h2>My First Angular 2 App</h2>{{data}} 
     <input type="button" (click)="doSomething()" value="Do Http"/>` 
}) 

export class ComponentOne { 
    constructor(public _http:Http){ 
    console.log("It is from ComponentOne constructor.."); 
     this._http = _http; 
    } 
    data:Object; 
    doSomething(){ 
     console.log("It is from doSomething ComponentOne"); 
     let temp:any = this._http.get('people.json')//people.json is not exist, intendedly maing any error 
    .map(res => res.json()) 
     .catch((err:any)=>{ console.log("Something is wrong..")});// If i keep this line i am getting errors like "Argument of type '(err:any) => void' is not assignable to parameter of type '(err: any, caught: Observable<any>) => Observable<{}>" 
     temp.subscribe(
     (res:any)=> {this.data = res._body; console.log(res)}, 
     (error:any)=>{ console.log("It isf from error..")},//It is not even getting called this block 
     () => console.log('thire,,,ddjfladjfljasdlfj'));//In one of the forum they suggested to use this 3rd perameter, this is also not working for me. 

    } 

} 

cevap

5

Böyle catch callback'inde gelen Observable nesneyi döndürmek için deneyebilirsiniz:

doSomething(){ 
    console.log("It is from doSomething ComponentOne"); 
    let temp:any = this._http.get('people.json') 
    .map(res => res.json()) 
    .catch((err:any) =>{ 
     console.log("Something is wrong.."); 
     return Observable.of(undefined); <== this line 
    }); 

    temp.subscribe(
    (res:any)=> {this.data = res._body; console.log(res)}, 
    (error:any)=>{ console.log("It isf from error..")}, 
    () => console.log('thire,,,ddjfladjfljasdlfj')); 
} 
+2

? Ben kendi fonksiyonumda bu hata ile başa çıkmak istiyorum, bu açısal – Blauhirn

+0

sıfır açıklama burada – Flame

8

Bu bu imzası olduğu gibi catch bloktan gözlemlenebilir dönmek gerekir. Yani burada

return Observable.throw(new Error(error.status)); 

deneyin

import {Observable} from 'rxjs/Rx'; 
... 

return this.http.request(new Request(this.requestoptions)) 
    .map((res: Response) => { 
     if (res) { 
      if (res.status === 201) { 
       return [{ status: res.status, json: res }] 
      } 
      else if (res.status === 200) { 
       return [{ status: res.status, json: res }] 
      } 
     } 
    }).catch((error: any) => { 
     if (error.status === 500) { 
      return Observable.throw(new Error(error.status)); 
     } 
     else if (error.status === 400) { 
      return Observable.throw(new Error(error.status)); 
     } 
     else if (error.status === 409) { 
      return Observable.throw(new Error(error.status)); 
     } 
     else if (error.status === 406) { 
      return Observable.throw(new Error(error.status)); 
     } 
    }); 
} 

ayrıca bkz

snippet'tir Bir gözlemlenebilir dönmek istemiyorum ne olur

+1

sıfır açıklama yapmanıza gerek yok Biz ideal bir dizi işlemek için durumların bir listesini olmalı ve sonra basit bir Ob.throw() sahip olmak için onunla karşılaştırmak gerekir . 500 hatası için, kullanıcı sunucuda bir sorun olduğu için hiçbir şey yapamaz, ancak 401 için kimlik bilgilerini ve benzerlerini kontrol etmeleri gerekir. – Nitin