I'll try to describe this as easy as possible
- "CompleteReservation" does a POST api call, and returns the ID from the newly saved object back, state "reservationId" is mutated with the new ID.
- A Redirect to "ReservationSummary" happens, and in the created event, it does a second API call which uses that ID as a parameter.. The problem is that the ID is always 0 (default value).
I know what the problem is, but i haven't found any solution to how to fix it yet : the store mutation is not finished, before created in "ReservationSummary" is called, makes it always 0.
FYI : I know that the value is sat (later on) through viewing Chrome VUE state..
How can i prevent this ?
CompleteReservation method (in Reservation.vue)
CompleteReservation: function() {
let newResObject = {
ResConfigId: this.id,
Name: this.reqName,
NrOfPersons: this.reqNrOfPersons,
Phone: this.reqPhone,
Email: this.reqEmail,
Address: this.reqAddress,
// ResDate: Utils.ConvertToStringDate(this.selectedDate),
ResDate: Utils.ConvertToUtc(this.selectedDate),
ResTime: this.availableSeats.ReservationStartHour,
}
this.$store.dispatch('completeReservation', newResObject).then( o => {
this.$router.push('/reservationsummary/')})
CompleteReservation action/mutation/getter/state (in store.js)
completeReservation({ commit }, newResObject) {
api.completeReservation(newResObject)
.then(reservationId => {
console.log("Reservasjonen er fullført..")
commit("UPDATE_RESERVATION_ID", reservationId)
})
}
// mutation
UPDATE_RESERVATION_ID(state, reservationId) { state.reservationId = reservationId },
// getter
reservationId: state => state.reservationId,
// state
reservationId: 0,
ReservationSummary.vue (computed and created)
export default {
computed: {
completedReservation() {
return this.$store.getters.completedReservation
}
},
created() {
console.log("Inside created...")
this.$store.dispatch('getReservationById')
},
methods: {
}
}
GetReservationById action in store.js
getters.reservationId is 0 here (default value)
getReservationById({ commit, getters }) {
api.getReservationById(getters.reservationId)
.then(completedReservation => {
console.log("Getting completed reservation")
commit("UPDATE_COMPLETED_RESERVATION", completedReservation)
})
},
Chrome VUE state after ReservationSummary.vue is loaded