可从主体观察

时间:2018-03-14 03:37:33

标签: rxjs observable ngrx

我正在尝试根据来自RX主题的更新创建操作

它正在运行,但我收到以下错误。

这是我的史诗

export function uploadSceneFile(action$, store) {
  return action$.ofType(CREATE_SCENE_SUCCESS)
    .mergeMap(({payload}) =>
      UploadSceneWithFile(payload)
        .subscribe(res => {
          if (res.progress > 0)
            store.dispatch(uploadSceneProgress(res))
          else if(res.progress === -1){
            store.dispatch(uploadSceneSuccess(res))
            requestSceneProcessing(res).map(res => {
            })
          }

        })
    )
}

这是主题

export function UploadSceneWithFile(scene){

  const subject$ = new Subject()

  const uploader = new S3Upload({
    getSignedUrl: getSignedUrl,
    uploadRequestHeaders: {'x-amz-acl': 'public-read'},
    contentType: scene.file.type,
    contentDisposition: 'auto',
    s3path: 'assets/',
    onError:()=>subject$.next('error'),
    onProgress: (val)=> subject$.next({...scene,progress:val}),
    onFinishS3Put: ()=>subject$.next({...scene,progress:-1}),
  })
  uploader.uploadFile(scene.file)

  return subject$
}

我从上一篇文章中读到,我应该使用.map,而不是.subscribe但如果我没有订阅就没有任何反应(上传不会发生)

这样做的最佳方法是什么?

subscribeToResult.js:74 Uncaught TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    at Object.subscribeToResult (subscribeToResult.js:74)
    at MergeMapSubscriber../node_modules/rxjs/operators/mergeMap.js.MergeMapSubscriber._innerSub (mergeMap.js:132)
    at MergeMapSubscriber../node_modules/rxjs/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:129)
    at MergeMapSubscriber../node_modules/rxjs/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:112)
    at MergeMapSubscriber../node_modules/rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
    at FilterSubscriber../node_modules/rxjs/operators/filter.js.FilterSubscriber._next (filter.js:89)
    at FilterSubscriber../node_modules/rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
    at Subject../node_modules/rxjs/Subject.js.Subject.next (Subject.js:55)
    at createEpicMiddleware.js:60
    at createEpicMiddleware.js:59
    at SafeSubscriber.dispatch [as _next] (applyMiddleware.js:35)
    at 

1 个答案:

答案 0 :(得分:1)

问题是您在mergeMap内订阅并返回无效的Subscription。回调只需要返回Observable,Promise,Array或Iterable。

我不确定你究竟需要做什么,但如果你需要执行一些副作用,你可以使用do()运算符而不是订阅。

export function uploadSceneFile(action$, store) {
  return action$.ofType(CREATE_SCENE_SUCCESS)
    .mergeMap(({ payload }) => UploadSceneWithFile(payload)
      .do(res => {
        ...
      })
    )
}

或者看起来您也可以在mergeMap之后执行操作:

export function uploadSceneFile(action$, store) {
  return action$.ofType(CREATE_SCENE_SUCCESS)
    .mergeMap(({ payload }) => UploadSceneWithFile(payload))
    .do(res => {
      ...
    });
}