gpt4 book ai didi

Angular 7 (RxJs 6.x) - 如何使用 throwError

转载 作者:太空狗 更新时间:2023-10-29 18:28:05 25 4
gpt4 key购买 nike

我想在我的订阅者上使用第二个参数(错误)但不起作用。

我的 Observable 代码:

return Observable.create(obs => {
cognitoidentityserviceprovider.adminCreateUser(params, function(error, data) {
if (error) {
console.log(error);
return throwError(error || 'Server error');
} else {
console.log(data);
return obs.next(data.User);
}
});
});

我的 console.log(error); 没问题,但什么也没有(没有痕迹)。

我的订阅者代码:

this.myService.createUser(user).subscribe(
result => this.getUsers(),
error => this.errorUsersProcessor(error));

我的订阅者的第二个参数(错误)永远不会调用。

最佳答案

来自文档:抛出错误

Creates an Observable that emits no items to the Observer and immediately emits > an error notification.

所以我的猜测是 throwError 正在向一个新的观察者发出一个错误,而不是你订阅的观察者。

你可以尝试通知你是真正的观察者来发出错误:

obs.error(error || new Error('Server error'));

关于Angular 7 (RxJs 6.x) - 如何使用 throwError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54686930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com