Как назначить (отфильтрованный) ответ на запрос Ax ios переменной в VueJS? - PullRequest
0 голосов
/ 12 июля 2020

У меня есть запрос Ax ios, ответом на который является объект:

enter image description here

I've filtered the response like so:

response.data.value_table_cols.map(col => col.header)

To get the following array as a result - In relation to the above image it's the data under value_table_rows:

["row 1", "row 2", "row 3"]

When you log response.data.value_table_cols.map(col => col.header) to the console it produces the above array. So, how can I assign response.data.value_table_cols.map(col => col.header) to a data variable called, say, allOfTheRows?

In a Vue JS page:

data: () => ({
      allOfTheRows: []
}),

My Axios request:

    getTable(table) {
        var self = this;

        axios.get(window.routes["value-tables.get"].replace("{value_table}", table.id))
            .then(function (response) {
                self.$refs.hotTableComponent.hotInstance.loadData(response.data.value_table_rows.map(row => row.value_table_cells));
                this.allOfTheRows = response.data.value_table_rows.map(row => row.header)
                console.log(response.data.value_table_rows.map(row => row.header));
            })

            .catch(function(error){
                console.error(error);
                if (!!error.response) console.log(error.response);
            });
    },

As instructed by the first answer I've rewritten the request like so:

    getTable(table) {
        var self = this;

        apiClient.get(window.routes["value-tables.get"].replace("{value_table}", table.id))
            .then(function (response) {
                self.$refs.hotTableComponent.hotInstance.loadData(response.data.value_table_rows.map(row => row.value_table_cells));
                this.allOfTheRows = response.data.value_table_rows.map(row => row.header)
            })

            .catch(function(error){
                console.error(error);
                if (!!error.response) console.log(error.response);
            });
    },

However it gave the following error:

введите описание изображения здесь

1 Ответ

0 голосов
/ 12 июля 2020

Выполнив:

apiClient.get(window.routes["value-tables.get"].replace("{value_table}", table.id))

        .then((response) => {
                   this.allOfTheRows = response.data.value_table_rows.map(row => row.value_table_cells));
             }

Обратите внимание , что я изменил функцию на стрелку, чтобы получить неявную привязку this

Кроме того, я бы сделал функция карты в отдельном методе, чтобы было место для некоторой проверки

...