[英]Catch the Error message text in a rxjs subscribe method in Angular
我在TypeScript中有一個nodeJS express服務器應用程序。 當發生錯誤時,我想在其中添加一些文本並在客戶端Angular應用程序中讀取它。
NodeJS Server,快遞代碼:
import {
Request,
Response
} from 'express';
if (medewerker) {
verifyPassword(medewerker.id, req.body.wachtwoord).then(isVerifiedPassword => {
if (isVerifiedPassword) {
.......
} else {
throw new Error('Wachtwoord niet juist ingevoerd');
}
}).catch(error => {
console.log(error);
res.status(401).json('I want to show this text: ' + error);
})
}
客戶代碼:
this.httpClient.put(this.url + 'medewerker/login', {
'email': authData.userId,
'wachtwoord': authData.password
}, {
headers: this.headertjes,
withCredentials: true,
observe: 'response',
responseType: 'json'
})
.subscribe((data1: HttpResponse < MedewerkerTypeId > ) => {
if (data1 != null) {
.....
}
}, ((error: HttpErrorResponse) => {
console.log(error.statusText) //here I want to grep and show my text.
}));
但我得到一個錯誤:未定義的消息。 我嘗試了所有選項,但只有error.status
和error.message
等默認屬性error.status
有效。 但是沒有像我想要的自定義消息。
也許有人可以幫助我?
可以使用error
屬性訪問ErrorResponse
的主體。
像這樣更改你的代碼:
this.httpClient.put(this.url + 'medewerker/login', {
'email': authData.userId,
'wachtwoord': authData.password
}, {
headers: this.headertjes,
withCredentials: true,
observe: 'response',
responseType: 'json'
})
.subscribe((data1: HttpResponse < MedewerkerTypeId > ) => {
if (data1 != null) {
.....
}
}, ((error: HttpErrorResponse) => {
console.log(error.error) //here error contains the text
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.