In my Vue JS 2 code, I have structured my data as follows:
data : {
newBus: {
name: '',
hours: {
sunday: '',
}
}
}
When setting the data using this input field:
<input v-model="newBus.hours.sunday" type="text" placeholder="Sunday">
Adding a business in this way is successful. However, when attempting to update the data by clicking on an item from a list and having the saved data fill the form, the following method is used:
<div v-for="bus in businesses" v-on:click="editBus(bus)" class="list-group-item bus-list-item">{{bus.name}}</div>
The editBus() method encounters the error:
vue.js:1453 TypeError: Cannot read property 'sunday' of undefined
This issue arises immediately upon clicking the item for updating. Any suggestions on how to tackle this?
EDIT: It seems that the error is linked to the absence of the hours
property in the bus
object. When checking with console.log(bus)
, it does not display. How can I incorporate this data into the business object without causing errors? It's puzzling because if it were not nested - i.e., sunday: ''
instead of hours: { sunday: ''}
- it functions correctly.
EDIT: For reference, here is the recreation on CodePen.