0

my web application breaks (won't load) when I use 'new Observable()' anywhere in a method or otherwise.

Angular version 2.1.0
RxJs 5.0.0-beta.12

These are my imports in the .ts-file:

import { Injectable } from '@angular/core';
import { DSUnit } from '../model/dsunit';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import 'rxjs/Rx';
import { DSCategory } from './../model/dscategory';
import { Observable } from 'rxjs';

When I have this piece of code it breaks:

private handleError(error: any) : Observable<any> {
    let errMsg = "argh";
    return new Observable(); // <-- "offending" code
}

with this error on the console of Chrome:

(SystemJS) XHR error (404 Not Found) loading http://localhost:3000/node_modules/rxjs(…)

And when I remove the offending line, and have this instead:

private handleError(error: any) : Observable<any> {
    let errMsg = "argh";
    return null; // <-- "works" 
}

my web application loads with no errors.. of course it breaks when the 'catch'-statement tries to handle an error, but that will work once I get past the hurdle with

"new Observable()"

Any ideas, anyone?

2
  • import { Observable } from 'rxjs/Rx'; This caught me out as well... Commented Oct 20, 2016 at 7:52
  • Sometimes you just can't trust your IDE when one's using the auto import feature.... Commented Oct 20, 2016 at 7:53

2 Answers 2

2

You could try this:

Observable.of(errMsg);

or

Observable.throw(errMsg);

depending on the caller.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work. The thing is that ANY use of Observable that will create an Observable-object will break. I initially tried Observable.throw but that broke, so I tried to return just a plain Observable object, which also breaks. Same goes when I try to use Observable.of()....
0

Damn it... the 'auto import' in Visual Studio Code had messed up my import.

When I import '{Observable}' it should not be

import {Observable} from 'rxjs'

it should be

import {Observable} from 'rxjs/Rx'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.