stasera
stasera

Reputation: 96

Vue getter returns undefined when page reload

I have a blog with some posts. When you click on the preview you will redirect on the page post. On the page of the post, I use a getter to load the correct post (I use the find function to return object.name which corresponds to the correct object in the array of objects).

const state = {
    ricettario: [], // data that contains all recipes (array of objects)
}

const actions = {
    // Bind State and Firestore collection
    init: firestoreAction(({ bindFirestoreRef }) => {
        bindFirestoreRef('ricettario', db.collection('____').orderBy('data'))
    })

const getters = {
    caricaRicetta(state) {
        console.log('Vuex Getter FIRED => ', state.ricettario)
        return nameParamByComponent => state.ricettario.find(ricetta => {
            return ricetta.name === nameParamByComponent
        })
    }
}

In the component, I call the getter in the computed property

computed: {
    ...mapGetters('ricettaStore', ['caricaRicetta']),
    ricetta() {
      return this.caricaRicetta(this.slug) // this.slug is the prop of the URL (by Router)
    }
  }

Anything goes in the right way but when I reload the page in the POST PAGE, the getter will fire 2 times:
1. return an error because the state is null
2. return the correct object
// screen below

enter image description here

So everything works fine from the front but not at all in the console and in the App.
I think the correct way is to call the getters in the created hook. What I've to change? It is a problem with the computed prop, getters or state?

POST PAGE:

<template>
  <div v-if="ricetta.validate === true" id="sezione-ricetta">
    <div class="container">
      <div class="row">
        <div class="col s12 m10 offset-m1 l8 offset-l2">
          <img
            class="img-fluid"
            :src="ricetta.img"
            :alt="'Ricetta ' + ricetta.titolo"
            :title="ricetta.titolo"
          />
        </div>
      </div>
    </div>
  </div>
  <div v-else>
      ...
  </div>
</template>

Upvotes: 0

Views: 1775

Answers (3)

Estus Flask
Estus Flask

Reputation: 222399

Database synchronization is asynchronous, ricettario is initially an empty array. Computed value is recomputed once synchronization is finished and ricettario array is filled, the component is updated.

Even if ricettario weren't empty, find may return undefined if it finds nothing. This needs to be handled where ricetta is used:

<div v-if="ricetta && ricetta.validate" id="sezione-ricetta">

Upvotes: 1

mare96
mare96

Reputation: 3859

You are trying to validate undifined property. So you need to check ricetta first.

Try like this:

<div v-if="ricetta && ricetta.validate === true" id="sezione-ricetta">

Upvotes: 1

Boris K
Boris K

Reputation: 1552

The error log is quite explicit, there is a xxx.validate somewhere in your Ricetta component template, but that xxx is undefined.

Because of this, your app crashes and stops working. I doubt it has anything to do with Vuex

Upvotes: 0

Related Questions