简体   繁体   中英

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.

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. 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

  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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable ERROR TypeError: You provided 'null' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable when deploying angular Angular5 : TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at <Jasmine> @ngrx/effects gives TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable in Angular Services Angular: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or 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