简体   繁体   English

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

[英]Subscribing to RxJs Observable with only onNext handler gives “this._subscribe is not a function”

I'm trying to subscribe to an Observable created from a Subject , and whenever I subscribe without an error handler I get the error this._subscribe is not a function . 我试图订阅从Subject创建的Observable ,每当我订阅时没有错误处理程序,我都会收到错误this._subscribe is not a function I found this question that described a similar error; 我发现这个问题描述了类似的错误; but the answers didn't seem to fit my situation. 但答案似乎并不符合我的情况。 Here is my code: 这是我的代码:

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

I also tried: 我也尝试过:

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

and I get the same error. 和我得到同样的错误。 Here is the stack trace when I try just subscribe : 这是我尝试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)  

and this is the stack trace when I try subscribeOnNext 这是我尝试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)

If I pass a trivial error handler like () => null to subscribe I don't get the error. 如果我通过一个普通的错误处理程序,例如() => null进行subscribe ,则不会收到该错误。 Any ideas? 有任何想法吗?

Thanks to user3743222 I figured it out. 多亏了user3743222,我知道了。 It turns out (logically actually) that you can't subscribe to a plain observable. 事实证明(从逻辑上来说)您不能订阅普通的可观察对象。 It needs to be constructed in a way such that it has values to observe. 它的构建方式必须使其具有值得观察的价值。 I changed the initialization for otherObservable in my unit tests from new Rx.Observable() to Rx.Observable.just(1) , and now it can be subscribed to. 我在单元测试otherObservable的初始化new Rx.Observable()更改为Rx.Observable.just(1) ,现在可以预订了。 I'm a little confused though why it didn't throw the same error when I passed both an onNext and onError handler. 我有点困惑,但是为什么当我同时传递onNextonError处理程序时它没有引发相同的错误。 But I'll take it. 但是我接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM