29
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

axiosのcatchでerror objectの中身を見れない

Posted at

問題

axiosのレスポンスを console.log() で表示した時
.then のレスポンスはうまくオブジェクトとして取れるのに
.catch のレスポンスは下記のように表示されてしまう

Error: Request failed with status code 422
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:59)

結論

catchのerrorオブジェクト?が文字列として表示されてしまっている。
なのでerror.responseと指定すればOK

axios.post('/test/create', this.formData)
  .then(response => { 
	console.log(response)
  })
  .catch(error => {
    console.log(error.response)
  });

or

axios.post('/test/create', this.formData)
  .then(response => { 
	console.log(response)
  })
  .catch(({response}) => {
    console.log(response)
  });

参考

https://katuo-ai.com/axios-error/
https://github.com/axios/axios/issues/960

29
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?