Bir hizmet çağrısından eşleme
yapmaya çalışıyorum ancak hata alıyorum.
https://stackoverflow.com/questions/41995647/subscribe-is-not-defined-in-angular-2 adresine baktım ve abone olmak için operatörlerin içinden dönmemiz gerektiğini söyledi. Benim de return ifadelerim var.
İşte kodum:
checkLogin(): Observable<boolean> {
return this.service.getData()
.map(
response => {
this.data = response;
this.checkservice = true;
return true;
},
error => {
// debugger;
this.router.navigate(['newpage']);
console.log(error);
return false;
}
)
.catch(e => {
return e;
});
}
Hata kaydı:
TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable
Örnek kodunuzda, map
operatörünüzün yalnızca bir çağrı alması gerekirken iki çağrı almasını sağlıyorsunuz. Hata işleme kodunuzu catch geri çağrısına taşıyabilirsiniz.
checkLogin():Observable<boolean>{
return this.service.getData()
.map(response => {
this.data = response;
this.checkservice=true;
return true;
})
.catch(error => {
this.router.navigate(['newpage']);
console.log(error);
return Observable.throw(error);
})
}
Ayrıca catch
ve throw
operatörlerini de içe aktarmanız gerekecektir.
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
EDIT:
Catch işleyicinizde Observable.throw
döndürerek, aslında hatayı yakalamayacağınızı unutmayın - hata yine de konsolda görünecektir.
Siz bir Observable
.map(response => <boolean>response.json())
Eğer sizin durumunuzda başka bir yaygın hizmet olan checkservice
kullanıyorsanız, basitçe
this.service.getData().subscribe(data=>console.log(data));
Bu, checkLogin()
işlevinizin dönüş türünü void yapacaktır
checkLogin():void{
this.service.getData()
.map(response => {
this.data = response;
this.checkservice=true;
}).subscribe(data=>{ });
ve koşulunuzu kontrol etmek için this.checkService
kullanabilirsiniz
Birim testimi yaparken tam olarak aynı hata mesajını aldım ve servislerimi taklit ettikten sonra gözlemlenebilir istisna attım.
Bunu Observable.throw
içinde tam işlevi ve biçimi geçirerek çözdüm.
Servisi çağıran ve veri almak için abone
olan gerçek kod. 400
hatasını işlemek için catch
olduğuna dikkat edin.
this.search(event).catch((e: Response) => {
if (e.status === 400) {
console.log(e.json().message);
} else if (e.url) {
console.log('HTTP Error: ' + e.status + ' ' + e.statusText,
'URL: ' + e.url, 'Info: ' + e.json().message));
}
}).finally(() => {
this.loading = false;
}).subscribe((bData) => {
this.data = bData;
});
Hizmetin içindeki kod
search() {
return this.someService.getData(request)
.do((r) => {
this.someService.defaultHeaders.delete('skipAlert');
return r;
})
.map((r) => {
return r.businessObjectDataElements.length && r.businessObjectDataElements || null;
});
}
SomeService ile alay ettim ve gözlemlenebilir verileri döndürdüm ve içinde gerekli tüm yöntemler olduğu için sorun yok.
someServiceApi = fixture.debugElement.injector.get(SomeService);
spyOn(someServiceApi, 'getData').and.returnValue(Observable.of({}));
Yukarıdaki kod tamam ama catch/error koşulunu Observable.throw({})
geçerek test etmeye çalıştığımda, servisten Response
tipi dönüş beklediği için bana hata gösteriyordu.
Bu yüzden aşağıdaki servis mocking dönüşü bana bu hatayı veriyordu.
someServiceApi.getData
.and.returnValue(Observable.throw(new Response({status: 400, body: [], message: 'not found error'})));
Bu yüzden, Response
tipi bir değer iletmek yerine geri dönüş nesnemde tam olarak beklenen işlevi çoğaltarak düzelttim.
someServiceApi.getData
.and.returnValue(Observable.throw({status: 400, json: () => { return {message: 'not found error'}}, body: []}));
// see `json: () => { return {message: 'not found error'}}` inside return value