admin管理员组文章数量:1290418
I am using the below methods to send data. I want to display an error response on my ponent. How can I console log error message in my ponent?
ponent.ts
signup(){
return this.loginservice.signupUser(this.model).subscribe(data => {
console.log(data.error);
});
}
service ts
signupUser(signupuserModel: any = {}):Observable<any>{
return this.http.post(`${this.signuouserurl}`,signupuserModel)
}
error message
I am using the below methods to send data. I want to display an error response on my ponent. How can I console log error message in my ponent?
ponent.ts
signup(){
return this.loginservice.signupUser(this.model).subscribe(data => {
console.log(data.error);
});
}
service ts
signupUser(signupuserModel: any = {}):Observable<any>{
return this.http.post(`${this.signuouserurl}`,signupuserModel)
}
error message
Share Improve this question edited Feb 26, 2019 at 7:08 fiza khan 1,29614 silver badges24 bronze badges asked Feb 26, 2019 at 4:23 kumarakumara 9374 gold badges23 silver badges46 bronze badges 1-
What is the output of
console.log(data.error);
– brk Commented Feb 26, 2019 at 4:24
3 Answers
Reset to default 5In RxJS, subscribe()
method can have 3 functions
next()
if observable emits value.error()
if there's an error thrown from the Observableplete()
if the observable is pleted.
What you need to do is to add an extra arrow function in your server call inside subscribe()
method
public error: any;
signup() {
return this.loginservice.signupUser(this.model).subscribe(success => {
console.log(success);
}, error => { // second parameter is to listen for error
console.log(error);
this.error = error;
});
}
If you want to show the error in your ponent.html, you can use the interpolation {{ }}
ponent.html
<div class="error">{{ error }}</div>
try
signup() {
return this.loginservice.signupUser(this.model).subscribe(data => {
console.log(data);
}, err => {
console.log(err);
});
}
You can also use try-catch approach in following way
async signup() {
try {
let data = await this.loginservice.signupUser(this.model).toPromise();
console.log(data)
} catch (e) {
console.log(e);
}
}
However not all http codes raise exception
you can choose any method to display an error.. on of the best way is seprate the success and error response with following code (for this your http call must thrown an exception if not then you have to chose the second option )
signup() {
return this.loginservice.signupUser(this.model).subscribe(success => {
console.log(success);
}, error => {
console.log(error);
});
}
or you can write conditional code like
signup() {
return this.loginservice.signupUser(this.model).subscribe(success => {
console.log(data);
if(success.status == 406){ console.log("success")}
else { console.log("Error ") }
}
}
本文标签: javascripthow to display http error message angular 6Stack Overflow
版权声明:本文标题:javascript - how to display http error message angular 6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741497701a2381909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论