问题
I am getting the following error which shows up in IE/Edge only.
Unable to get propety '0' of undefined or null reference
I am using RxJs observables in the following manner:
getVersions(): void {
this.databaseService.getProductVersions().subscribe(
(versions) => this.processVersions(versions),
(error) => console.error(error)
);
}
and in Chrome/Firefox this works without errors. productVersions looks like this:
getProductVersions(): Observable<any[]> {
let options = this.header();
let url: string = this.baseUrl + 'admin/version/searchall';
return this.http.get(url, options).map(
response => response.json(),
error => console.log(error)
);
}
Am I using the Observable incorrectly. Should I be unsubscribing? (My api call is finite so from what I've read unsubscribing is not necessary).
回答1:
As much, as I know, .map operator cannot handle errors, so there's no usage of it. RxJS provides .catch method which's responsible for it.
Try to make fix on it, If that would not make things right, we will find another solution. I'm out of IDE to test it by my own, so forgive me. =)
回答2:
From the image you provided that the error is raised in the __tryOrUnsub method which is an internal method called when unsubscribing (or completing) an observer.
The question is why it happens and I'm suspicious that the error comes from the this.processVersions(...) call. Otherwise your code looks fine, just be aware that the second parameter to the map() operator is used to set custom this context (see http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-map).
In you code, you're setting this to a function (error) => console.error(error) but this shouldn't be a problem.
回答3:
I thought I had ruled out the 3rd party library I was using. But I found that it was happening there. So it was not my code causing the issue. Thanks @Martin and @Jakub for your help. I am at least now using the map/catch methods correctly thanks to you.
来源:https://stackoverflow.com/questions/43058203/unable-to-get-property-0-of-undefined-or-null-reference