【发布时间】:2020-02-03 01:03:09
【问题描述】:
当我的axios调用失败时,catch()中的代码正确显示err.response.data.message中包含的错误信息,这是定义的。
但是当axios 调用成功时,我在控制台中得到这个错误:
Uncaught (in promise) TypeError: Cannot read property 'data' of undefined
如何解决?
这是我的axios 电话代码:
mounted () {
this.$axios.$get(`http://example.com/wp-json/project/v1/post/${this.$route.params.id}`)
.then((res) => {
this.title = res.post_title
this.content = res.post_content
})
.catch((err) => {
this.$toast.error(err.response.data.message)
})
【问题讨论】:
-
你
console.log()err的内容了吗?正如错误消息非常清楚地指出的那样,err.response未定义。 -
是的,无论 axios 调用是否成功,它都不会起作用。但是当 axios 调用失败时 $toast.error 可以正常工作。很奇怪