Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RXJS: TypeError: this._subscribe is not a function

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

  • There are two places in the RXJS code, were I was able to find this._subscribe: Observable.ts Line 203 and Observable.ts Line 208.
like image 250
tmuecksch Avatar asked Nov 10 '17 20:11

tmuecksch


2 Answers

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);
like image 137
tmuecksch Avatar answered Nov 17 '22 10:11

tmuecksch


I was getting exactly same error message in one of my angular application while doing upgrade when I replaced Observable.of(someval as any) with new Observable(someval as any).

Error: Uncaught (in promise): TypeError: this._subscribe is not a function

I also tried to make it Observable.create(someval as any) but the same error.

Solution

Replacing it with of(someval as any) resolved the problem for me.

import { of } from 'rxjs';

// use it just like this
of(retval as any);

detailed error message

ERROR Error: Uncaught (in promise): TypeError: this._subscribe is not a function TypeError: this._subscribe is not a function at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:42) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28) at MapOperator.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapOperator.call (map.js:18) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:23) at MapOperator.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapOperator.call

like image 21
Aniruddha Das Avatar answered Nov 17 '22 09:11

Aniruddha Das