私は完全なスタックアプリケーションのためのangular2フロントエンドを構築しています。私は、ユーザーのログインに取り組んでいると私は、ログインフォームが送信されたときに呼び出され、この機能があります:次のように私のUserServiceの中にログイン機能があるAngular2エラーを返すHTTPサービスで観察可能charによってchar
onSubmit(email, password) {
this.userService.login(this.user.email, this.user.password).subscribe((result) => {
console.log('reps' + result)
if (result) {
this.router.navigate(['']);
}
});
}
を:
login(_email, _password) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let data = { email: _email, password: _password };
console.log(data);
return this.http.post('/api/login', data, options)
.map(this.extractData)
.map((res) => {
console.log('res' + JSON.stringify(res))
if (res.token) {
localStorage.setItem('auth_token', res.token);
this.loggedIn = true;
}
return true;
}).catch(this.handleError);
}
そして最後に、handleErrorの機能:
private handleError(error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
console.log('isinError Handler' + error);
let errMsg: string;
if (error instanceof Response) {
const err = error || JSON.stringify(error);
console.log('err' + err); // Prints out "Response with status: 401 Unauthorized for URL: http://localhost:3000/api/login"
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error('msg' + errMsg); // Prints out "401 - Unauthorized Response with status: 401 Unauthorized for URL: http://localhost:3000/api/login"
return errMsg;
}
私は間違ったパスワードを送信すると、私はエラーを取得:
401 - Unauthorized Response with status: 401 Unauthorized for URL: http://localhost:3000/api/login
エラーハンドラの変数errMsg
から印刷されています。これは素晴らしいですが、私のコンポーネントに戻って、resp4
、resp0
、resp1
...を出力します。すなわちcharによるerrMsg char。これをどのようにして1つのフルストリングに戻すことができますか?