Código:
export default { data() { return { nameCity: '', } }, methods: { findCity(event){ event.preventDefault() findCityCust().then(function(response) { console.log(response) this.nameCity = response; }) }, }, }Y aquí - this.nameCity = respuesta; - arroja un error Uncaught (en promesa) TypeError: No se pueden leer las propiedades de undefined
¿Cómo trabajar con campos de métodos asincrónicos en Vue 3?
el error es causado por this
diferencias-entre-flecha-y-funciones-regulares: este valor
en function(){} , this es el objeto global
in () => {} , this es la instancia actual de Vue
así que cámbialo a
findCityCust().then(response => { console.log(response) this.nameCity = response; })o
methods: { async findCity(event){ event.preventDefault() this.nameCity = await findCityCust(); }, },La función estándar no está vinculada al componente, pruebe la función de flecha:
export default { data() { return { nameCity: '', } }, methods: { findCity(event){ event.preventDefault() findCityCust().then((response) => { console.log(response) this.nameCity = response; }) }, }, }