RXJS: TypeError: this._subscribe is not a function

tmuecksch picture tmuecksch · Nov 10, 2017 · Viewed 7.2k times · Source

I'm migrating an ionic 3.8 app to 3.9.2. This migration includes an update to RXJS 5.5

I'm now experiencing this error:

TypeError: this._subscribe is not a function. (In 'this._subscribe(sink)', 'this._subscribe' is an instance of t)

After hours of debugging, I found out that this code portion is related to the error:

protected observeConnectionState() {

    // rxjs/observable/of
    of(new Event('disconnect'))
        .pipe(
            // rxjs/operators/merge
            merge(connect$),
            merge(disconnect$),

            // Map eventname to string (rxjs/operators/map)
            map((e: IEvent) => {
                return e.eventName == 'connect' ? 'connected' : 'disconnected';
            })
        )
        // Apply to class context
        .subscribe((newConnectionState) => {
            // this.connectionState$ is a BehaviorSubject
            this.connectionState$.next(newConnectionState);
        });
}

ADDITIONAL INFO

Answer

tmuecksch picture tmuecksch · Nov 11, 2017

Well, I found the problem. And it's not related to Cordova.

For other people encountering this problem: Forget the stack trace - it's useless. In my case in a subscriber of this.connectionState$ I tried to create an Observable from a promise. But I did it wrong.

This is what was wrong:

import { Observable } from 'rxjs/Observable';
//...
const myObservable$ = Observable.create(myPromise);

This is how it should be done:

import { fromPromise } from 'rxjs/observable/fromPromise';
// ...
const myObservable$ = fromPromise(myPromise);