[英]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.