前回の404エラーの質問から続けての投稿になります。
Ionicのログイン機能をFlicher Blog様のフロントエンドの実装方法とバックエンドの実装方法実装しているのですが、422エラーが表示されデータを送信することが出来ません。
自分では原因を特定できなかったのですが、どのあたりが原因と考えられるでしょうか。
エラーコード
console
1POST http://ドメイン/Laravel-ionic/public/api/auth/register 422 (Unprocessable Entity)
エラーに関連すると思われるコード
auth.service.ts
TypeScript
1import { HttpClient, HttpHeaders } from '@angular/common/http'; 2import { Injectable } from '@angular/core'; 3import { tap } from 'rxjs/operators'; 4import { NativeStorage } from '@ionic-native/native-storage/ngx'; 5import { EnvService } from './env.service'; 6import { User } from '../models/user'; 7@Injectable({ 8 providedIn: 'root' 9}) 10export class AuthService { 11 isLoggedIn = false; 12 token:any; 13 constructor( 14 private http: HttpClient, 15 private storage: NativeStorage, 16 private env: EnvService, 17 ) { } 18 login(email: String, password: String) { 19 return this.http.post(this.env.API_URL + 'auth/login', 20 {email: email, password: password} 21 ).pipe( 22 tap(token => { 23 this.storage.setItem('token', token) 24 .then( 25 () => { 26 console.log('Token Stored'); 27 }, 28 error => console.error('Error storing item', error) 29 ); 30 this.token = token; 31 this.isLoggedIn = true; 32 return token; 33 }), 34 ); 35 } 36 register(fName: String, lName: String, email: String, password: String) { 37 return this.http.post(this.env.API_URL + 'auth/register', 38 {fName: fName, lName: lName, email: email, password: password} 39 ) 40 } 41 logout() { 42 const headers = new HttpHeaders({ 43 'Authorization': this.token["token_type"]+" "+this.token["access_token"] 44 }); 45 return this.http.get(this.env.API_URL + 'auth/logout', { headers: headers }) 46 .pipe( 47 tap(data => { 48 this.storage.remove("token"); 49 this.isLoggedIn = false; 50 delete this.token; 51 return data; 52 }) 53 ) 54 } 55 user() { 56 const headers = new HttpHeaders({ 57 'Authorization': this.token["token_type"]+" "+this.token["access_token"] 58 }); 59 return this.http.get<User>(this.env.API_URL + 'auth/user', { headers: headers }) 60 .pipe( 61 tap(user => { 62 return user; 63 }) 64 ) 65 } 66 getToken() { 67 return this.storage.getItem('token').then( 68 data => { 69 this.token = data; 70 if(this.token != null) { 71 this.isLoggedIn=true; 72 } else { 73 this.isLoggedIn=false; 74 } 75 }, 76 error => { 77 this.token = null; 78 this.isLoggedIn=false; 79 } 80 ); 81 } 82}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/21 10:40
2019/06/23 11:45