Good morning everyone.
I have been struggling for several days on an app I'm trying to build for some experience. I have done quite a lot but am now stuck in the edit page as I just cannot grasp the state management side.
So here is my problem.
I have a button in my ProfilePage.vue that if I click on it sends me to the EditInvoice.vue page.
<button @click="onSubmit"><router-link to="/edit-invoice">Edit</router-link></button>
my store.js state:
state: {
invoice: [],
},
Then in my store.js, I have the following in my actions:
actions: {
invoiceCollection({commit}) {
database.collection('invoices')
.get()
.then((querySnapShot) => {
querySnapShot.forEach((doc) => {
const curInvData = doc.data();
commit('invoice', curInvData);
})
})
}
},
This action gets the data I need from firestore and should look like this.
clientDetails: "Adress"
dateCreated: "September 15th 2019"
invoice: Array(2)
invoiceSubTotal: "R 167,50"
invoiceTotal: (...)
itemPaid: (...)
userId: (...)
userName: (...)
I then mutate my state (store.js):
mutations: {
invoice: (state, payload) => state.invoice = payload,
},
and then use a getter (store.js):
getters: {
// Get Invoice data from state
invoice: state => {
return state.invoice
},
},
I then import mapGetters into my component (EditInvoice.vue) ...iterate through my getter's under my computed property with ...mapGetters(['invoice']),
and then use a simple function with a console log and use a lifecycle hook.
created() {
this.currentInvoice();
},
methods: {
...mapActions(['invoiceCollection']),
currentInvoice() {
console.log(this.invoice)
},
I'm very new to programming and would just like to know, why my getters, and everything else always returns an empty Observer
[__ob__: Observer]
length: 0
__ob__: Observer {value: Array(0), dep: Dep, vmCount: 0}
__proto__: Array
on the first few attempts. And then after a few clicks on the edit button in ProfilePage.vue eventually shows the right data
I have been searching for the same issue and have found several cases but none have helped me. This is a simplified version that I stripped. All I want to know is why my state is not persistent. The data is there it's just not showing on the first or second-page load.
Any help will be greatly appreciated.