Почему я получаю это неопределенное сообщение об ошибке свойства в моем обещании? - PullRequest
0 голосов
/ 04 октября 2019

Когда мой вызов axios завершается неудачно, код в catch() правильно отображает сообщение об ошибке, содержащееся в err.response.data.message, которое определено.

Но когда вызов axios завершается успешно, Я получаю эту ошибку в консоли:

Uncaught (в обещании) TypeError: Невозможно прочитать свойство 'data' из неопределенного

Как это исправить?

Вот мой 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)
    })

1 Ответ

0 голосов
/ 06 октября 2019

Попробуйте res.data.post_title вместо res.post_title

...
.then((res) => {
  this.title = res.data.post_title
  this.content = res.data.post_content
})
...

Axios возвращает объект, содержащий следующую информацию

{
  data: {},        // the response that was provided by the server
  status: 200,     // the HTTP status code from the server response
  statusText: 'OK',// the HTTP status message from the server response
  headers: {},     // the headers that the server responded with
  config: {},      // the config that was provided to `axios` for the request
  request: {}      // the request that generated this response
}

Если вы воруете, получаете ту же ошибку, то это, вероятно,неправильно отформатированные данные на стороне сервера, попробуйте console.log ответа и посмотрите, есть ли какая-либо проблема на стороне обслуживания.

...
.then((response) => {
    console.log(response.data);
    console.log(response.status);
    console.log(response.statusText);
    console.log(response.headers);
    console.log(response.config);
  });
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...