question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Observable.throw not defined in Webpack-version of CLI-defined service

See original GitHub issue

I’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.

  1. Windows 7
  2. angular-cli: 1.0.0-beta.11-webpack, node: 4.4.7, os: win32 x64
  3. 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
    }

}
  1. Observable.throw is undefined.

TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.throw is not a function

Refer to this StackOverflow post.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

73reactions
CalvinDalecommented, Aug 11, 2016

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.

39reactions
debbabi-nadercommented, May 3, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found