简体   繁体   English

类型错误:您在需要流的地方提供了“未定义”。 你可以提供一个 Observable、Promise、Array 或 Iterable

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

I have an issue with catching error in Ionic based with Angular.我在基于 Angular 的 Ionic 中捕获错误时遇到问题。

I am on create() method trying to create new User and if username already exists, i retrieve response from backend but on my method error throws the named message in title.我正在使用 create() 方法尝试创建新用户,如果用户名已经存在,我会从后端检索响应,但在我的方法错误中会在标题中抛出命名消息。 I have tried most of the similar answers but still stuck我已经尝试了大部分类似的答案,但仍然卡住

any help任何帮助

Config.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账户.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, I didnt want to spam to much code with toastService I have created, but toastService is working 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');
          }
        }
      );

If you find no account, your map won't be able to return an Account, and you will not get into to catch if the request is OK.如果找不到帐户,您的地图将无法返回帐户,如果请求正常,您将无法捕捉。

To fix it, you could add a check on the response and if no account is found then, you throw an exception要修复它,您可以添加对响应的检查,如果没有找到帐户,则抛出异常

暂无
暂无

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

相关问题 类型错误:您提供了一个预期流的无效对象。 你可以提供一个 Observable、Promise、Array 或 Iterable - TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable 错误类型错误:您在需要流的地方提供了“空”。 你可以提供一个 Observable、Promise、Array 或 Iterable - ERROR TypeError: You provided 'null' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable 您提供了“未定义”的预期流。 您可以提供一个Observable,Promise,Array或Iterable - You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable TypeError:您在需要 stream 的地方提供了“未定义”。 部署angular时可以提供Observable、Promise、Array或Iterable - TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable when deploying angular Angular5:TypeError:您提供了&#39;undefined&#39;,其中包含一个流。 您可以提供Observable,Promise,Array或Iterable - Angular5 : TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable TypeError:您在预期 stream 的地方提供了“未定义”。 您可以提供 Observable、Promise、Array 或 Iterable。 在<jasmine></jasmine> - TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at <Jasmine> @ngrx / effects给出了TypeError:你提供了&#39;undefined&#39;来预期流。 您可以提供Observable,Promise,Array或Iterable - @ngrx/effects gives TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable 错误类型错误:您在需要流的地方提供了“未定义”。 您可以在 Angular 服务中提供 Observable、Promise、Array 或 Iterable - ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable in Angular Services Angular:您在需要流的地方提供了一个无效的对象。 你可以提供一个 Observable、Promise、Array 或 Iterable - Angular: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable xplat您提供了一个无效的对象,该对象应在其中流。 您可以提供一个Observable,Promise,Array或Iterable - xplat You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM