E
E
eywsf2021-11-10 10:46:22
Vue.js
eywsf, 2021-11-10 10:46:22

How to access data fields in Vue from an async method?

There is a code:

export default {
   data() {
        return {
            nameCity: '',
        }
    },
    methods: {
        findCity(event){
            event.preventDefault()
            findCityCust().then(function(response) { 
                console.log(response)
               this.nameCity = response;
            })
        }, 
    },
}

And here - this.nameCity = response; - throws Uncaught (in promise) TypeError: Cannot read properties of undefined

How to work with fields from asynchronous methods in Vue 3?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AndromedaStar, 2021-11-10
@eywsf

export default {
   data() {
        return {
            nameCity: '',
        }
    },
    methods: {
        findCity(event){
            event.preventDefault()
            findCityCust().then((response) => { 
                console.log(response)
               this.nameCity = response;
            })
        }, 
    },
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question