简体   繁体   中英

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 . 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 :

  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

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. Any ideas?

Thanks to user3743222 I figured it out. 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. I'm a little confused though why it didn't throw the same error when I passed both an onNext and onError handler. But I'll take it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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