Angular 2 - Possible to subscribe to http errors in the 'calling' component? -


i'm having trouble retrieving error service. component calling service , want subscribe errors might returned. however, seems i'm not allowed subscribe on calling method.

public getmethods(v) {     if(v != null) {         return this._http.get('http:localhost/testservice', {             method: 'get',             headers: new headers([                 'accept', 'application/json',                 'content-type', 'application/json'             ])         })         .map(res => (res.json()))         .map((method: any) => {             if(method) {                 let result: array<string> = [];                 var list = method.list;                  list.foreach(v => {                     result.push(v);                 })                 return result;             }         })         .subscribe(data => {             this._datastore.mn= data;             this._mnobserver.next(this._datastore.mn);         },         error => {             **// want retrieve error in 'calling' component**             return error;         });     }     else {         console.error("get names. v null");     } 

in component: not work:

this._mnservice.getmethods(v), error => {       alert("error in da house"); }; 

this not work:

this._mnservice.getmethods(v).subscribe( error => {       alert("error in da house"); }); 

so, work? thanks! :)

you need refactor bit method:

public getmethods(v) {   if(v != null) {     return this._http.get('http:localhost/testservice', {         method: 'get',         headers: new headers([             'accept', 'application/json',             'content-type', 'application/json'         ])     })     .map(res => (res.json()))     .map((method: any) => {         if(method) {             let result: array<string> = [];             var list = method.list;              list.foreach(v => {                 result.push(v);             })             return result;         }     });   } else {     console.error("get names. v null");   } } 

this way receive error when subscribing on observable:

this._mnservice.getmethods(v).subscribe((data) => {   }, error => {     alert("error in da house");   }); 

what bit strange in code subscribe in getmethods method. don't return observable request subscription.

if want trigger _mnobserver when response there, leverage do operator instead. here sample:

public getmethods(v) {   if(v != null) {     return this._http.get('http:localhost/testservice', {         method: 'get',         headers: new headers([             'accept', 'application/json',             'content-type', 'application/json'         ])     })     .map(res => (res.json()))     .map((method: any) => {         if(method) {             let result: array<string> = [];             var list = method.list;              list.foreach(v => {                 result.push(v);             })             return result;         }     })     .do(data => { // <-------       this._datastore.mn= data;       this._mnobserver.next(this._datastore.mn);     });   } else {     console.error("get names. v null");   } } 

Comments