[Vuejs]-Array is undefined from Veux Store

2πŸ‘

βœ…

This happens because tweakwiseSortedProducts is not undified but an empty list. You can try:

tweakwiseHasProducts () {
  if (this.$store.state.tweakwise?.tweakwiseSortedProducts?.length !== 0) {
    return (
      this.$store.state.tweakwise.tweakwiseSortedProducts[0].noProducts ===
      false
    );
  }
  return false;
},

or just:

tweakwiseHasProducts () {
  return this.$store.state.tweakwise?.tweakwiseSortedProducts[0]?.noProducts === false;
},

which will be false if any of this elements is undefined, or true if noProducts is really false

πŸ‘€Gergo Miklos

1πŸ‘

It is recommended to use getter when calling a value in Vuex.
Please refer to the following.

  getters: {
    getTweakwiseSortedProducts: (state: any) => {
      return state.tweakwise?.tweakwiseSortedProducts || [];
    },
  },

tweakwiseHasProducts () {    
  this.$store.getters.getTweakwiseSortedProducts.length ? true : false;
}
πŸ‘€k22pr

Leave a comment