Angular6 在angular 6中获取错误时执行Subscribe
最近我将angular 5升级为angular 6,因此我将服务代码升级如下:Angular6 在angular 6中获取错误时执行Subscribe,angular6,rxjs6,Angular6,Rxjs6,最近我将angular 5升级为angular 6,因此我将服务代码升级如下: register(postBody: any): Observable<any> { return this.http .post(this.remoteUrl + "auth/register", postBody) .pipe( catchError(this.handleError("register", [])) ); } this.authService.register(
register(postBody: any): Observable<any> {
return this.http
.post(this.remoteUrl + "auth/register", postBody)
.pipe(
catchError(this.handleError("register", []))
);
}
this.authService.register(this.formObj.value).subscribe(
response => {
}
)
寄存器(postBody:any):可观察{
返回此文件。http
.post(this.remoteUrl+“auth/register”,postBody)
.烟斗(
catchError(this.handleError(“寄存器”,[]))
);
}
this.authService.register(this.formObj.value).subscribe(
响应=>{
}
)
现在,当我从API中得到400个错误时。我能够在错误处理程序中捕获此错误,但仍然执行订阅,为什么?只有在无错误地获得响应时,才能调用Subscribe
我对这一点还不熟悉,但在《angular 5》中,这并没有发生。那么,有人能纠正我的错误吗?Subscribe允许您处理注册函数返回的任何内容。但是,为什么您只是捕获错误呢?您至少应该映射您的结果,并在成功时返回它。我也不认为你应该做的是接球、投掷和失误 我希望这对你有帮助
register(postBody: any): Observable<any> {
return this.http
.post(this.remoteUrl + "auth/register", postBody)
.pipe(
map(data => data), //this will return the response when success
catchError(this.handleError(err =>{
//Here you can do something like
err.code === 404
? throwError("Not found")
: throwError(err)
})
);
}
this.authService.register(this.formObj.value).subscribe(
response => {
// Do stuff whith your result
},
err => {
// Do stuff whith your error
},
() => {
// Do stuff after completion
},
)
寄存器(postBody:any):可观察{
返回此文件。http
.post(this.remoteUrl+“auth/register”,postBody)
.烟斗(
map(data=>data),//成功时返回响应
catchError(this.handleError(err=>{
//在这里你可以做一些类似的事情
错误代码===404
?投掷者(“未找到”)
:投掷者(错误)
})
);
}
this.authService.register(this.formObj.value).subscribe(
响应=>{
//做有结果的事情
},
错误=>{
//做有错误的事
},
() => {
//完成后做些事情
},
)
Subscribe允许您处理从注册函数返回的任何内容。但是,为什么您只是捕获错误?您至少应该映射结果并在成功时返回它。而且我不认为您应该做什么来捕获和抛出错误
我希望这对你有帮助
register(postBody: any): Observable<any> {
return this.http
.post(this.remoteUrl + "auth/register", postBody)
.pipe(
map(data => data), //this will return the response when success
catchError(this.handleError(err =>{
//Here you can do something like
err.code === 404
? throwError("Not found")
: throwError(err)
})
);
}
this.authService.register(this.formObj.value).subscribe(
response => {
// Do stuff whith your result
},
err => {
// Do stuff whith your error
},
() => {
// Do stuff after completion
},
)
寄存器(postBody:any):可观察{
返回此文件。http
.post(this.remoteUrl+“auth/register”,postBody)
.烟斗(
map(data=>data),//成功时返回响应
catchError(this.handleError(err=>{
//在这里你可以做一些类似的事情
错误代码===404
?投掷者(“未找到”)
:投掷者(错误)
})
);
}
this.authService.register(this.formObj.value).subscribe(
响应=>{
//做有结果的事情
},
错误=>{
//做有错误的事
},
() => {
//完成后做些事情
},
)
handleError做什么?如果它返回一个of
notthrow
observable,那就吞下了错误。谢谢,伙计。你是对的。handleError
做什么?如果它返回一个of
notthrow
observable,那就吞下了错误。谢谢,你是对的。这是正确的答案。为什么没有标记为正确?这是正确答案。为什么没有标记为正确?