簡體   English   中英

類型錯誤:您在需要流的地方提供了“未定義”。 你可以提供一個 Observable、Promise、Array 或 Iterable

[英]TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

我在基於 Angular 的 Ionic 中捕獲錯誤時遇到問題。

我正在使用 create() 方法嘗試創建新用戶,如果用戶名已經存在,我會從后端檢索響應,但在我的方法錯誤中會在標題中拋出命名消息。 我已經嘗試了大部分類似的答案,但仍然卡住

任何幫助

配置文件

import {HttpErrorResponse} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';

export default class Config {

  static handleError(error: HttpErrorResponse): Observable<any> {
    console.log('*****handleErrors*****');
    console.log(error); //TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    console.log(error.message); //TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    console.log(error.error.message);

    return throwError(
      error.error.message());
  }
}

賬戶.service.ts

  create(account: Account): Observable<Account> {
    return this.httpClient
      .post<ResponseWrapper>(this.accountsUrl, JSON.stringify(account), this.httpOptions).pipe(
        map(rw => {
          return rw.data;
        }),
        catchError(
          this.handleError
        )
      );
  }

 handleError(error: Response | any) {
    return Config.handleError(error);
  }

account-detail.page.ts,我不想用我創建的 toastService 向太多代碼發送垃圾郵件,但是 toastService 正在工作

this.accountService.create(this.selectedAccount).subscribe(
        res => {
          this.selectedAccount = res;
          const io = new InteractionObject('save', 'account', this.selectedAccount);
          this.accountDetailEvent.emit(io);
          this.interactionService.setSave(io);
          setTimeout(() => {

          }, 500);
          this.toastService.showSaveSuccessMsg();
        },
        error => { //TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
          this.errorMsg = error;
          if ('ACCOUNT_USERNAME_EXISTS' === this.errorMsg) {
            this.toastService.showSaveFailMsg('account_username_exists');
          }
        }
      );

如果找不到帳戶,您的地圖將無法返回帳戶,如果請求正常,您將無法捕捉。

要修復它,您可以添加對響應的檢查,如果沒有找到帳戶,則拋出異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 類型錯誤:您提供了一個預期流的無效對象。 你可以提供一個 Observable、Promise、Array 或 Iterable 錯誤類型錯誤:您在需要流的地方提供了“空”。 你可以提供一個 Observable、Promise、Array 或 Iterable 您提供了“未定義”的預期流。 您可以提供一個Observable,Promise,Array或Iterable TypeError:您在需要 stream 的地方提供了“未定義”。 部署angular時可以提供Observable、Promise、Array或Iterable Angular5:TypeError:您提供了&#39;undefined&#39;,其中包含一個流。 您可以提供Observable,Promise,Array或Iterable TypeError:您在預期 stream 的地方提供了“未定義”。 您可以提供 Observable、Promise、Array 或 Iterable。 在<jasmine></jasmine> @ngrx / effects給出了TypeError:你提供了&#39;undefined&#39;來預期流。 您可以提供Observable,Promise,Array或Iterable 錯誤類型錯誤:您在需要流的地方提供了“未定義”。 您可以在 Angular 服務中提供 Observable、Promise、Array 或 Iterable Angular:您在需要流的地方提供了一個無效的對象。 你可以提供一個 Observable、Promise、Array 或 Iterable xplat您提供了一個無效的對象,該對象應在其中流。 您可以提供一個Observable,Promise,Array或Iterable
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM