tamafull / devise_token_auth_trial

0 stars 0 forks source link

エラーメッセージ #13

Closed tamafull closed 2 years ago

tamafull commented 2 years ago

axiosのcatchで

        }).catch(function (error) {
          if (error.response.status == 401) this.message = 'メールアドレスかパスワードが間違っています'
          else console.log(error)
        })

こんな風にしたら

Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'message')

が出た これはfunctionだとエラーをthrowしたところのthisを引き継いでいないため なのでこうする

        }).catch((error) => {
          if (error.response.status == 401) this.message = 'メールアドレスかパスワードが間違っています'
          else console.log(error)
        })

アロー関数を用いることでメソッド内で用いていたthis=Vueインスタンスを参照できるようになる

tamafull commented 2 years ago

できればフォーム右側にエラーメッセージを表示したい サーバー側の応答によって判別するのはできないためクライアント側で入力バリデーションする

tamafull commented 2 years ago

なぜかHome画面で405が出てる(APIはsign_in)のでこれも直す

Started GET "/api/v1/home" for ::1 at 2021-12-04 15:53:03 +0900
Processing by Api::V1::HomeController#index as HTML
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms | Allocations: 206)

Started GET "/api/v1/auth/sign_in" for ::1 at 2021-12-04 15:53:03 +0900
Processing by Api::V1::Auth::SessionsController#new as HTML
Completed 405 Method Not Allowed in 0ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 269)

homeで認証失敗してるのはなぜか

tamafull commented 2 years ago

devise_groupの使い方が原因っぽい 適当にやってしまった

tamafull commented 2 years ago

33

こっちでやる