简体   繁体   English

捕获并返回返回错误时,Rxjs 可观察抛出错误

[英]Rxjs observable throw error when returns error is caught and returned

Hi I am getting this error "you provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable" when this http request is called from my effects.嗨,当从我的效果中调用此 http 请求时,我收到此错误“您提供了一个预期流的无效对象。您可以提供 Observable、Promise、Array 或 Iterable”。

deleteAccount(accountId: string): Observable<any> {
    return this.http.delete(FUNDING_ACCOUNT_DELETE_URL
      .replace(ACCOUNT_ID, accountId)).pipe(
        timeout(2000),
        catchError(err => { 
          return err; })
      );
  }

@Effect()
  paymentOptionRemove = this.actions
    .ofType(ActionTypes.PAYMENT_OPTION_REMOVE).pipe(
      switchMap((action: PaymentOptionRemoveAction) =>
        this.service.deleteAccount(action.payload).pipe(
          map(
            _ => new PaymentOptionRemovedAction()),
          // tslint:disable-next-line:arrow-return-shorthand
          catchError(err => {
            if (err) {
              console.log(err);
            }
            return of(new PaymentOptionRemoveErrorAction());
          })
        ))
    );

From the deleteAccount function, you are supposed to return an Observable .deleteAccount函数中,您应该返回一个Observable In the catchError block, you are trying to return the err , which is not observable.catchError块中,您试图返回err ,这是不可观察的。

Use throwError from rxjs :使用rxjs throwError

throwError is a function from rxjs that creates an Observable that emits only error to the subscriber. throwError是来自 rxjs 的一个函数,它创建一个Observable ,它只向订阅者发出错误。 Designed for use case such as you have.专为您拥有的用例而设计。

import { throwError } from 'rxjs';

Inside the delete function, use like:在删除函数中,使用如下:

return throwError(err);  

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

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