【问题标题】:TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or IterableTypeError:您在预期流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable
【发布时间】:2021-12-10 11:47:56
【问题描述】:

我在基于 Angular 的 Ionic 中捕获错误时遇到问题。

我正在尝试创建新用户的 create() 方法,如果用户名已经存在,我会从后端检索响应,但我的方法错误会在标题中抛出命名消息。我已经尝试了大多数类似的答案,但仍然卡住了

任何帮助

配置.ts

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());
  }
}

account.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');
          }
        }
      );

【问题讨论】:

    标签: angular typescript ionic-framework


    【解决方案1】:

    如果响应中没有数据,您的地图将无法返回帐户。如果请求为“OK”,您将无法捕捉到。

    要修复它,您可以在响应中添加检查,如果没有找到帐户,您可以尝试抛出异常

    尝试添加日志以查看您的 rw.data 是否设置

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 2023-03-13
      • 2017-09-18
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多