【发布时间】:2018-02-04 13:30:11
【问题描述】:
我使用 HTTP API 通过 json 返回应用级错误,其中 status == 'error'。
在我的注册服务中,我这样做:
return this.userService
.create(user)
.map(
json => {
console.log(json);
if (json.status=='error') {
console.log('error return throw');
return Observable.throw('Credentials mismatch or not existing');
}
const user = json.data as User;
return user;
},
err => {
console.log(err);
}
);
在我的 singUp.component 中,我这样做了:
onSubmit() {
// blahblah
this.si
.signUp(this.credentials.emailAddress, this.credentials.password, this.credentials.zipCode)
.subscribe(
user => {
console.log(user);
console.log(`SignUpComponent.onSubmit() found user.userId ${user.userId}`);
this.router.navigate(['signin']);
},
err => {
this.errorMessage = `*** SignUpComponent: Error while signing up {err}`;
console.error('*** SignUpComponent: Error while signing up', err);
this.resume();
//this.router.navigate(['signup']);
});
this.ngOnChanges();
}
不幸的是,当服务返回错误(使用 Observable.throw())时,组件不会触发 err 闭包,而是用户闭包将 Observable.throw 作为用户参数传递。
我希望我能触发 err 关闭。
我错过了什么?
更新:这是我得到的:
[Log] SigninService.signUp, zipCode=
[Log] {data: null, status: "error", message: "Error user create: - missing id API usage v1b3: Tu…ate\"}↵post: []↵.↵", ws: "v1b3: Tuesday, August 25th 2017 20h20 @ Tanger"}
[Log] error return throw
[Log] ErrorObservable {_isScalar: false, error: "Credentials mismatch or not existing", scheduler: undefined, _subscribe: function, …}
[Log] SignUpComponent.onSubmit() found user.userId undefined
【问题讨论】:
-
你的
.map()在做什么? -
它会查找 API 状态以确保 API 返回全新用户(当 status=='success' 时)。如果状态等于“错误”(代表未创建用户帐户),我希望“为订阅者生成错误”。
-
这行得通吗?
-
我在问题中添加了日志
标签: angular observable throw