0

I'm having this code in the blade:

<list-categories-component></list-categories-component>

and this template with vue code

<template>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Example Component</div>
                <div class="card-body">
                    <ul>
                    <li v-for="cat in cats" :key="cat.cat_id">{{cat.cat_en_title}}</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
export default {
    data(){
        return{
            cats:[],
        };
    },
    created() {
        this.fetchTaskList();
    },
    methods:{
        fetchTaskList(){
            axios.get('/api/listCats')
                .then(function (response) {
                    console.log(response.data.cats);
                    this.cats = response.data.cats;

                })
                .catch(function (error) {
                    // handle error
                    console.log(error);
                });
        }

    }
}
</script>

and here's the axios response

Axios response

and here's the log

console logs

but data can't be rendered successfully as it find an error here

this.cats = response.data.cats;

I changed this.cats = response.data.cats to this.cats = response.data to catch the array itself but still getting the same error.

I'm running npm run watch so what's the problem here and how to fix it?

1 Answer 1

1

Change your functions inside then and catch to arrow functions, in order to read this as the vue component:

methods:{
        fetchTaskList(){
            axios.get('/api/listCats')
                .then(response => {
                    console.log(response.data.cats);
                    this.cats = response.data.cats;

                })
                .catch( error => {
                    // handle error
                    console.log(error);
                });
        }

}
1
  • 1
    Thanks a lot. I really appreciate it.
    – PHP User
    Commented May 31, 2020 at 21:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.