仅使用onNext处理程序订阅RxJs Observable会给出“this._subscribe不是函数”

时间:2016-06-29 15:28:21

标签: javascript rxjs observable

我正在尝试订阅从Observable创建的Subject,每当我订阅没有错误处理程序时,我都会收到错误this._subscribe is not a function。我发现this问题描述了类似的错误;但答案似乎不适合我的情况。这是我的代码:

const subject = new Rx.Subject();
subject
    .withLatestFrom(otherObservable)
    .subscribe(
        values => {
            // some logic
       }
    );

我也尝试过:

const subject = new Rx.Subject();
subject
    .withLatestFrom(otherObservable)
    .subscribeOnNext(
        values => {
            // some logic
       }
    ); 

我得到同样的错误。当我尝试subscribe时,这是堆栈跟踪:

  Observable.Rx.Observable.observableProto.subscribe.observableProto.forEach (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2034:19)                                                                        
WithLatestFromObservable.subscribeCore (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:4084:33)                                                                                                              
WithLatestFromObservable.tryCatcher (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:63:31)                                                                                                                   
setDisposable [as action] (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2082:46)                                                                                                                           
ScheduledItem.invokeCore (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:896:33)                                                                                                                             
ScheduledItem.invoke (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:884:40)                                                                                                                                 
runTrampoline (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:1125:37)                                                                                                                                       
tryCatcher (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:63:31)                                                                                                                                            
CurrentThreadScheduler.schedule (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:1141:45)                                                                                                                     
WithLatestFromObservable.Rx.ObservableBase.ObservableBase._subscribe (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2095:32)                                                                                
WithLatestFromObservable.Rx.Observable.observableProto.subscribe.observableProto.forEach (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2034:19)  

这是我尝试subscribeOnNext

时的堆栈跟踪
Observable.Rx.Observable.observableProto.subscribe.observableProto.forEach (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2034:19)
    WithLatestFromObservable.subscribeCore (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:4084:33)
    WithLatestFromObservable.tryCatcher (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:63:31)
    setDisposable [as action] (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2082:46)
    ScheduledItem.invokeCore (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:896:33)
    ScheduledItem.invoke (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:884:40)
    runTrampoline (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:1125:37)
    tryCatcher (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:63:31)
    CurrentThreadScheduler.schedule (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:1141:45)
    WithLatestFromObservable.Rx.ObservableBase.ObservableBase._subscribe (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2095:32)
    WithLatestFromObservable.Rx.Observable.observableProto.subscribeOnNext (/home/ryan/code/redurx/node_modules/rx/dist/rx.js:2046:19)

如果我将() => null之类的简单错误处理程序传递给subscribe,我就不会收到错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

感谢user3743222 我明白了。事实证明(实际上逻辑上)您无法订阅普通的可观察对象。它需要以一种值得观察的方式构建。我将单元测试中otherObservable的初始化从new Rx.Observable()更改为Rx.Observable.just(1),现在可以订阅。我有点困惑,但是当我通过onNextonError处理程序时,为什么它没有抛出同样的错误。但我会接受它。