I've been encountering some issues with vuex getters.
My objective is to arrange the cart
data, which consists of an array of objects, by date
using the getter named myCartItems
.
The problem I'm facing is that when I add a second payload
{prod_id: 2, date: Wed Nov 03 2021 10:40:20}
in my Details
component, the props['prod_id'] ends up logging as prod_id: 1
instead of prod_id: 2
Mutation:
addToCart: (state, payload) => {
state.cart.push(payload) // payload = {prod_id: 1, date: Wed Nov 03 2021 10:37:26}
}
Getters:
cartItems: (state) => {
return state.cart.slice().sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
);
},
HTML:
<div v-for="(item,index) in cartItems" :key="index">
<Details :id="item.prod_id" />
</div>