You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

六眼飞鱼酱① 提交于 2019-12-12 17:26:18

问题


Say I have the following function references:

const externalRequests = (params) => Rx.Observable.zip(
  externalApi1(params),
  externalApi2(params)
)

and

const internalDB = (params) => Rx.Observable.fromPromise(getStuffFromDB(params)

externalRequests returns something of the shape:

{ _isScalar: false,
  source: 
   { _isScalar: false,
     source: { _isScalar: false, array: [Object], scheduler: undefined },
     operator: { project: [Object] } },
  operator: undefined }

and can be .subscribe'd to.

internalDB, when .then'd directly returns the right data from the database, and when wrapped in Rx.Observable.fromPromise as above, returns something of the shape:

{ _p: 
   { handle: 115,
     type: 'promise',
     className: 'Object',
     constructorFunction: { ref: 122 },
     protoObject: { ref: 123 },
     prototypeObject: { ref: 1 },
     status: 'pending',
     promiseValue: { ref: 1 },
     properties: [],
     internalProperties: [ [Object], [Object] ],
     text: '#<Promise>' },
  _s: {} }

, which, when .subscribe'd directly returns something like so:

{ isStopped: false,
  observer: 
   { isStopped: false,
     _onNext: [Function: val],
     _onError: [Function: val],
     _onCompleted: [Function: val] },
  m: 
   { isDisposed: false,
     current: { isDisposed: false, current: null } } }

Say I now want to flatten all of this into a single stream: so I write something like:

Rx.Observable.zip(externalRequests, internalDB).mergeAll().subsribe(() => console.log('potato'))

And receive an exception of the form: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

To summarize: What is the correct way for me to cast my promise as an Observable, and how do I merge/flatten multiple observables as above?


回答1:


As @martin mentioned you give functions to the zip operator, not observables.

You need to call those functions with the needed params for your code to work.

Also, I don't think you need the mergeAll call, as the result of internalDB and externalRequests calls are only observables, not higher order observables (observables containing observables)

Rx.Observable
  //    ▼ you need to call the functions in order to get their observables
  .zip(externalRequests(params), internalDB(params))
  .subscribe(() => console.log('potato'))



回答2:


usually it's because you imported zip from 'rxjs/operators'

you should import zip from 'rxjs/observable'




回答3:


my mistake to import file like this...

import {loginEpic} from "./Epic";

and i just correct this like...

import loginEpic from "./Epic";


来源:https://stackoverflow.com/questions/44633266/you-provided-an-invalid-object-where-a-stream-was-expected-you-can-provide-an-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!