Answer the question
In order to leave comments, you need to log in
Why does Vue.js return __ob__: Observer?
API:
created () {
this.loadProfile()
},
getPosts(author) {
return axios.get(`${options.serverUrl}/posts/${author}`)
},
async loadPosts () {
const response = await services.getPosts(this.$route.params.id)
this.posts = response.data
},
Answer the question
In order to leave comments, you need to log in
axios.get does not return a response, but a Promise
Therefore, we should try:
async loadPosts () {
await services.getPosts(this.$route.params.id).then((response) => {this.posts = response.data})
},
Pavel Kornilov wrote everything correctly, the get method of axios is asynchronous, it returns a promise.
Therefore, the getPosts method also needs to be made asynchronous:
async getPosts(author) {
try {
const { data } = await axios.get(`${options.serverUrl}/posts/${author}`);
return data;
} catch(error) {
return error; // важный момент, что будет возвращать функция при ошибках выполнения запроса на сервер
}
},
async loadPosts () {
this.loading = true; // вешаем лоадер на момент асинхронного запроса
try {
const { data } = await axios.get(`${options.serverUrl}/posts/${this.$route.params.id}`);
this.posts = data;
this.loading = false;
} catch(error) {
// делаем что-то в случае ошибки
this.loading = false;
}
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question