Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
How to access data fields in Vue from async method?

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 an error Uncaught (in promise) TypeError: Cannot read properties of undefined

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

the error is caused by this

differences-between-arrow-and-regular-functions: this value

in function(){}, this is the global object

in () => {}, this is the current Vue instance

so change it to

findCityCust().then(response => { 
    console.log(response)
    this.nameCity = response;
})

or

methods: {
    async findCity(event){
        event.preventDefault()
        this.nameCity = await findCityCust();
    }, 
},
over 4 years ago · Santiago Trujillo Report

0

Standard function is not binded to component, try arrow function:

export default {
   data() {
        return {
            nameCity: '',
        }
    },
    methods: {
        findCity(event){
            event.preventDefault()
            findCityCust().then((response) => { 
                console.log(response)
               this.nameCity = response;
            })
        }, 
    },
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!