Observable.throw not defined in Webpack-version of CLI-defined service
See original GitHub issueI’m not sure if this is an Angular CLI issue or not. But I thought I would bring it to your attention.
Services generated using the Webpack version of the Angular CLI version do not have the Observable.throw
method defined at runtime.
- Windows 7
- angular-cli: 1.0.0-beta.11-webpack, node: 4.4.7, os: win32 x64
- Use that CLI to create a service that performs a GET request and triggers a 404 server error.
import { Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
@Injectable()
export class ApiService {
constructor(private http: Http) { }
fetchRemoteData(): Observable<any> {
const invalidUrl = 'https://api.somedomain.com/an-invalid-path';
return this.http.get(invalidUrl)
.map((response: Response) => {
return <any>response.json();
})
.catch(this.handleServerError);
}
private handleServerError(error: Response) {
return Observable.throw(error.json().error || 'Server error'); // Observable.throw() is undefined at runtime using Webpack
}
}
Observable.throw
isundefined
.
TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.throw is not a function
Refer to this StackOverflow post.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9
Top Results From Across the Web
"rxjs" observable.throw is not a function - Angular4
I'm trying to use "rxjs" catch and throw but I've got an undefined function error in my console. Try importing import { Observable...
Read more >rxjs__WEBPACK_IMPORTED_M...
It was showing an error: Observable.throw is not a function. My Serivce Call code was given below: import { Injectable } from '@angular/core ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oops, a simple mistake. I solved this by adding
import 'rxjs/add/observable/throw';
. I had this line commented out (though not included in this issue’s submission) because I was using/operator/
rather than/observable/
which doesn’t exist.I solved this by using: import { Observable } from ‘rxjs/Rx’; Instead of: import { Observable } from ‘rxjs/Observable’;
Answer also available at: https://github.com/ReactiveX/rxjs/issues/1866