【发布时间】: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