0

I'm currently using a vue. I'm having a problem with my data, because when it inside the $.each loop it becomes undefined.

export default{

    data() {
        return {
            list: ['sample 1', 'sample 2']
        }
    },

    methods: {
        updateList(){
            this.form.get('/tasks/get')
                .then(data => {

                    console.log(this.list); //displays all list

                    $.each(data.data, function(key, value) {
                        console.log(this.list); //returns undefined
                    });
                }
        }
    }
}

Someone knows how to fix this?

schutte
  • 1,949
  • 7
  • 25
  • 45
  • You are using a "classic" `function`, which overrides `this`. You can simply use an arrow function. Replace `function(key, value) {` with `(key, value) => {`. You can read more here : https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – Seblor Feb 03 '20 at 15:49
  • @Seblor sorry, I'm kind of beginner of `vuejs`. I don't get your idea Sir, can you show an example Sir. – schutte Feb 03 '20 at 15:53
  • 1
    `$.each(data.data, (key, value) => console.log(this.list));` See the linked duplicate posts for more context. – thanksd Feb 03 '20 at 15:54
  • @gecko This is not related to Vue. This is simply how JavaScript works. For more details, see the questions linked by thanksd – Seblor Feb 03 '20 at 15:55
  • @thanksd thanks Sir it really helped. thanks a lot – schutte Feb 03 '20 at 15:56

0 Answers0