In the midst of a small project that involves utilizing nuxt, js, and axios, I encountered an issue when attempting to assign the response data to my formFields
object. Despite having declared formFields
within the data section, I kept receiving an error message stating 'undefined' in the console. Here is a snippet of my code:
Code snippet:
editCustomers (customerId, submit = false) {
this.editMode = true
this.customerId = customerId
if (submit === 1) {
// this.$Progress.start()
this.$axios.$post('mydomain.com' + customerId + '/1', $('#add-customer').serialize()).then(function (data) {
this.validation(data)
// another form validation again using the helper
this.refresh = true
})
// this.$Progress.finish()
} else {
this.$axios.$get('mydomain.com' + customerId).then(function (data) {
this.formFields = data.customers[0]
})
}
}
Variables in my data section:
data () {
return {
laravelData: {},
formFields: {},
search: null,
editMode: true,
customerId: null,
refresh: false
}
}
Despite correctly declaring the data variables, the assignment
this.formFields = data.customers[0]
resulted in the following error message:
Uncaught (in promise) TypeError: Cannot set property 'formFields' of undefined