RXJS: TypeError: this._subscribe is not a function
RXJS: TypeError: this._subscribe is not a function
我正在将 ionic 3.8 应用程序迁移到 3.9.2。
此迁移包括对 RXJS 5.5
的更新
我现在遇到这个错误:
TypeError: this._subscribe is not a function. (In 'this._subscribe(sink)', 'this._subscribe' is an instance of t)
经过几个小时的调试,我发现这段代码部分与错误有关:
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);
});
}
附加信息
- RXJS 代码中有两个地方,我能找到
this._subscribe
: Observable.ts Line 203 and Observable.ts Line 208.
嗯,我找到问题了。而且它与Cordova无关。
对于遇到此问题的其他人:
忘记堆栈跟踪 - 它没用。在我的例子中,在 this.connectionState$
的订阅者中,我试图从承诺中创建一个 Observable。但是我做错了。
这是错误的地方:
import { Observable } from 'rxjs/Observable';
//...
const myObservable$ = Observable.create(myPromise);
应该这样做:
import { fromPromise } from 'rxjs/observable/fromPromise';
// ...
const myObservable$ = fromPromise(myPromise);
当我将 Observable.of(someval as any)
替换为 new Observable(someval as any)
时,我在我的 angular 应用程序之一中收到完全相同的错误消息。
错误:未捕获(承诺):TypeError:this._subscribe 不是函数
我也试过Observable.create(someval as any)
但是同样的错误。
解决方案
用 of(someval as any)
替换它解决了我的问题。
import { of } from 'rxjs';
// use it just like this
of(retval as any);
详细错误信息
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
我正在将 ionic 3.8 应用程序迁移到 3.9.2。 此迁移包括对 RXJS 5.5
的更新我现在遇到这个错误:
TypeError: this._subscribe is not a function. (In 'this._subscribe(sink)', 'this._subscribe' is an instance of t)
经过几个小时的调试,我发现这段代码部分与错误有关:
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);
});
}
附加信息
- RXJS 代码中有两个地方,我能找到
this._subscribe
: Observable.ts Line 203 and Observable.ts Line 208.
嗯,我找到问题了。而且它与Cordova无关。
对于遇到此问题的其他人:
忘记堆栈跟踪 - 它没用。在我的例子中,在 this.connectionState$
的订阅者中,我试图从承诺中创建一个 Observable。但是我做错了。
这是错误的地方:
import { Observable } from 'rxjs/Observable';
//...
const myObservable$ = Observable.create(myPromise);
应该这样做:
import { fromPromise } from 'rxjs/observable/fromPromise';
// ...
const myObservable$ = fromPromise(myPromise);
当我将 Observable.of(someval as any)
替换为 new Observable(someval as any)
时,我在我的 angular 应用程序之一中收到完全相同的错误消息。
错误:未捕获(承诺):TypeError:this._subscribe 不是函数
我也试过Observable.create(someval as any)
但是同样的错误。
解决方案
用 of(someval as any)
替换它解决了我的问题。
import { of } from 'rxjs';
// use it just like this
of(retval as any);
详细错误信息
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